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
Post a Comment