Created
May 12, 2015 07:36
-
-
Save msaroufim/bd44137d52613892aca0 to your computer and use it in GitHub Desktop.
argument unpacking
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
>>> range(3, 6) # normal call with separate arguments | |
[3, 4, 5] | |
>>> args = [3, 6] | |
>>> range(*args) # call with arguments unpacked from a list | |
[3, 4, 5] | |
In the same fashion, dictionaries can deliver keyword arguments with the **-operator: | |
>>> | |
>>> def parrot(voltage, state='a stiff', action='voom'): | |
... print "-- This parrot wouldn't", action, | |
... print "if you put", voltage, "volts through it.", | |
... print "E's", state, "!" | |
... | |
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} | |
>>> parrot(**d) | |
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment