Monday, July 10, 2017

python function arguments

Python function parameters is like this

*non-keywords is a tuple
** keywords is a dict
You can call the method like newfoo(2, 4, *(6, 8), **{'foo': 10, 'bar': 12})
In this case, normal_arg1 = 2, normal_arg2 = 4, nonkeywords is (6,8), keywords is {'foo': 10, 'bar': 12}
newfoo(1)
    get error
newfoo(1,2 )
    normal_arg1 =1, normal_arg2 = 2
newfoo(*(1,2))
    normal_arg1 =1, normal_arg2 = 2
newfoo(1, *(2,))
    normal_arg1 =1, normal_arg2 = 2
newfoo(1, *(2,3))
    normal_arg1 =1, normal_arg2 = 2, nonkeywords: (3,)
newfoo(1, 2, x=3)
    normal_arg1 =1, normal_arg2 = 2, keywords: {'x':3}

For named arguments, the sequence can be changed.
test1(100,200) is equivalent to test1(x=100,y=200) and is equivalent to test1(y=200, x=100)
test1(100, y=200) is also allowed
but test1(100, x=200) is not allowed

For mixed arguments, named arguments have to follow default arguments, you cannot do this
Change it to

You can call test2(1,2,3) which is equivalent to test2(x=3, *(1,2))

To print all input arguments

No comments:

Post a Comment