Created
March 18, 2012 04:57
-
-
Save chobits/2069014 to your computer and use it in GitHub Desktop.
python *args, **kwargs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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