Python short course: Arguments of functions in Python

In python functions, the arguments with default values are placed at the last which follows other arguments without default values. 


In [1]: def func_test_mult(a, b=3):
   ...:     """This is a test func for arguments
   ...:     Args:
   ...:         a: numbers 1
   ...:         b: numbers 2
   ...:      return:
   ...:         multiplication for two numbers"""
   ...:     return a * b
   ...: 

In [2]: func_test_mult(4)
Out[2]: 12

The func behaves well, but if we put the default value at the first place, while the 'a' at the last. Then the python will complains this


In [3]: def func_test_mult(b=3, a):
   ...:     """This is a test func for arguments
   ...:     Args:
   ...:         a: numbers 1
   ...:         b: numbers 2
   ...:      return:
   ...:         multiplication for two numbers"""
   ...:     return a * b
   ...: 
  File "<ipython-input-3-6c948b8837ea>", line 1
    def func_test_mult(b=3, a):
                       ^
SyntaxError: non-default argument follows default argument

Comments

Popular posts from this blog

Lab's weekly topic - week 50 2023 - Detailed explanation for encoder of U-Net

Lab's recommendation: Awesome books to learn machine learning and AI (continue updating)