Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # func_args_and_kwargs.py
- def foo(a, b=3, *args, **kwargs):
- print a, b, args, kwargs
- # Now, b will be explicitly set if you pass it as a keyword argument or the second positional argument
- # Examples:
- x,y,z,c,k = 1,2,3,4,5
- foo(x) # a=x, b=3, args=(), kwargs={}
- foo(x, y) # a=x, b=y, args=(), kwargs={}
- foo(x, b=y) # a=x, b=y, args=(), kwargs={}
- foo(x, y, z, k) # a=x, b=y, args=(z, k), kwargs={}
- foo(x, c=y, d=k) # a=x, b=3, args=(), kwargs={'c': y, 'd': k}
- foo(x, c=y, b=z, d=k) # a=x, b=z, args=(), kwargs={'c': y, 'd': k}
- # foo(x, y, b=z) ### TypeError: foo() got multiple values for keyword argument 'b'
- # Note: In particular, foo(x, y, b=z) doesn't work because b is assigned by position in that case
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement