## Monday, July 10, 2017

### python function arguments

Python function parameters is like this
```
def newfoo(normal_arg1, normal_arg2, *nonkeywords, **keywords):
pass
```

*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.
```
def test1(x=1,y=2):
pass
```
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
```
def test2(a, x=1, b):
pass
```
Change it to
```
def test2(a, b, x=1):
pass
```

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

To print all input arguments
```
def func(a, b, c, *x, **y):
print(locals()) # this can print all input arguments
```