Skip to content

Instantly share code, notes, and snippets.

@chobits
Created March 18, 2012 04:57
Show Gist options
  • Save chobits/2069014 to your computer and use it in GitHub Desktop.
Save chobits/2069014 to your computer and use it in GitHub Desktop.
python *args, **kwargs
>>> def test(a, b):
... print a, b
...
>>> test('hello', 'world')
hello world
>>> test(*('hello', 'world')) # tuple
hello world
>>> test(*iter(['hello', 'world'])) # iter
hello world
>>> test(*['hello', 'world']) # list
hello world
>>> test(*['hello', 'world', '!'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes exactly 2 arguments (3 given)
>>>
>>> test(a = 'hello', b = 'world')
hello world
>>> test(**{'a':'hello', 'b':'world'}) # dict
hello world
>>> test(*{'hello':'???', 'world':'???'}) # list
world hello
>>> test(*{'hello':'???', 'world':'???'}.keys()) # list
world hello
>>>
>>>
>>> def test(*args, **kwargs):
... print args
... print kwargs
...
>>> test('hello', 'world', key0 = 'hello', key1 = 'world')
('hello', 'world')
{'key1': 'world', 'key0': 'hello'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment