Created
March 13, 2013 01:24
-
-
Save tjstebbing/5148656 to your computer and use it in GitHub Desktop.
A basic tutorial on arguments in python :)
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
line = '\n'+80*'*'+'\n' #formatting helper | |
# A quick overview of argument handling in python | |
# ************************************************************************ | |
# In python, functions have arguments which are both positional and keyword | |
# arguments, that is you can call a function using implicit positions to assign | |
# values to an argument, or you can call them with specific arguments. | |
# Arguments can also have defaults in the function definition, as with the | |
# argument 'd' here, note that default arguments can only happen at the end | |
# after non-default arguments ie: def foo(a=1, b, c, d) is a syntax error: | |
def foo(a, b, c, d=4): | |
print 'a', a | |
print 'b', b | |
print 'c', c | |
print 'd', d | |
print line + "Regular arguments" + line | |
print "positional arguments with default" | |
foo(1, 2, 3) | |
print "\npositional arguments overriding default" | |
foo(2, 3, 4, 5) | |
# ************************************************************************ | |
# Python has special syntax for dealing with arguments as lists and dicts, | |
# lists for positional arguments, dicts for keyword arguments. This syntax | |
# is * for positional arguments and ** for keyword arguments. | |
def bar(*positionalArguments, **keywordArguments): | |
print 'positional', positionalArguments | |
print 'keyword', keywordArguments | |
print line + "* and ** argument capturing" + line | |
print "\ncapturing only positional arguments" | |
bar(1,2,3,4) | |
print "\ncapturing only keyword arguments" | |
bar(a=1, b=2) | |
print "\ncapturing positional and keyword arguments" | |
bar(1, 2, c=3, d=4) | |
# ************************************************************************ | |
# You can also mix these two techniques to capture some arguments explicitly, | |
# and others with * and **. | |
def baz(a, b=1, *positionalArguments, **keywordArguments): | |
print 'a', a | |
print 'b', b | |
print 'positional', positionalArguments | |
print 'keyword', keywordArguments | |
print line + "Mixing regular, * and ** argument capturing" + line | |
print "\nproviding only the minimum argument" | |
baz(1) | |
print "\nproviding a, b and some positional and keyword arguments" | |
baz(1, 2, 3, 4, 5, x="hello", y="world") | |
# Notice in the last instance, a and b do not get captured by the * and **. | |
# ************************************************************************ | |
# Finally, the * and ** syntax serves a second purpse, when you call a function, | |
# you can use the same syntax to expand list and dict values into positional and | |
# keyword arguments. lets use the functions we've defined above and call them | |
# with some lists of values: | |
print line + "Using * and ** argument injection" + line | |
print "\nFilling all positional arguments from a list" | |
myArgs = ['I', '<3', 'skunks', '^_^'] | |
foo(*myArgs) | |
print "\nFilling positional and keyword arguments from a list and a dict" | |
myArgs = ["hope", "you've", "found"] | |
myKWArgs = { 'this' : 'tutorial', 'kinda' : 'helpful!'} | |
bar(*myArgs, **myKWArgs) | |
# Ende~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment