Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> def print_keyword_args(**kwargs):
- ... # kwargs is a dict of the keyword args passed to the function
- ... for key, value in kwargs.iteritems():
- ... print "%s = %s" % (key, value)
- ...
- >>> print_keyword_args(first_name="John", last_name="Doe")
- first_name = John
- last_name = Doe
- >>> kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'}
- >>> print_keyword_args(**kwargs)
- first_name = Bobby
- last_name = Smith
- func(a=1, b=2, c=3)
- args = {'a': 1, 'b': 2, 'c':3}
- func(**args)
- args = {'name': person.name}
- if hasattr(person, "address"):
- args["address"] = person.address
- func(**args) # either expanded to func(name=person.name) or
- # func(name=person.name, address=person.address)
- def setstyle(**styles):
- for key, value in styles.iteritems(): # styles is a regular dictionary
- setattr(someobject, key, value)
- setstyle(color="red", bold=False)
- def myDo(what, where, why):
- if what == 'swim':
- doSwim(where, why)
- elif what == 'walk':
- doWalk(where, why)
- ...
- elif what == 'drive':
- doDrive(where, why, vehicle)
- def myDo(what, where, why, **kwargs):
- if what == 'drive':
- doDrive(where, why, **kwargs)
- elif what == 'swim':
- doSwim(where, why, **kwargs)
- def f(a = 0, *args, **kwargs):
- print("Received by f(a, *args, **kwargs)")
- print("=> f(a=%s, args=%s, kwargs=%s" % (a, args, kwargs))
- print("Calling g(10, 11, 12, *args, d = 13, e = 14, **kwargs)")
- g(10, 11, 12, *args, d = 13, e = 14, **kwargs)
- def g(f, g = 0, *args, **kwargs):
- print("Received by g(f, g = 0, *args, **kwargs)")
- print("=> g(f=%s, g=%s, args=%s, kwargs=%s)" % (f, g, args, kwargs))
- print("Calling f(1, 2, 3, 4, b = 5, c = 6)")
- f(1, 2, 3, 4, b = 5, c = 6)
- Calling f(1, 2, 3, 4, b = 5, c = 6)
- Received by f(a, *args, **kwargs)
- => f(a=1, args=(2, 3, 4), kwargs={'c': 6, 'b': 5}
- Calling g(10, 11, 12, *args, d = 13, e = 14, **kwargs)
- Received by g(f, g = 0, *args, **kwargs)
- => g(f=10, g=11, args=(12, 2, 3, 4), kwargs={'c': 6, 'b': 5, 'e': 14, 'd': 13})
- def args_kwargs_test(arg1, arg2, arg3):
- print "arg1:", arg1
- print "arg2:", arg2
- print "arg3:", arg3
- #args can either be a "list" or "tuple"
- >>> args = ("two", 3, 5)
- >>> args_kwargs_test(*args)
- #keyword argument "kwargs" has to be a dictionary
- >>> kwargs = {"arg3":3, "arg2":'two', "arg1":5}
- >>> args_kwargs_test(**kwargs)
- def test(**kwargs):
- print kwargs['a']
- print kwargs['b']
- print kwargs['c']
- args = { 'b': 2, 'c': 3}
- test( a=1, **args )
- 1
- 2
- 3
- def function1(param1,param2="arg2",param3="arg3"):
- print("n"+str(param1)+" "+str(param2)+" "+str(param3)+"n")
- function1(1) #1 arg2 arg3 #1 positional arg
- function1(param1=1) #1 arg2 arg3 #1 named arg
- function1(1,param2=2) #1 2 arg3 #1 positional arg, 1 named arg
- function1(param1=1,param2=2) #1 2 arg3 #2 named args
- function1(param2=2, param1=1) #1 2 arg3 #2 named args out of order
- function1(1, param3=3, param2=2) #1 2 3 #
- #function1() #invalid: required argument missing
- #function1(param2=2,1) #invalid: SyntaxError: non-keyword arg after keyword arg
- #function1(1,param1=11) #invalid: TypeError: function1() got multiple values for argument 'param1'
- #function1(param4=4) #invalid: TypeError: function1() got an unexpected keyword argument 'param4'
- def function2(param1, *tupleParams, param2, param3, **dictionaryParams):
- print("param1: "+ param1)
- print("param2: "+ param2)
- print("param3: "+ param3)
- print("custom tuple params","-"*10)
- for p in tupleParams:
- print(str(p) + ",")
- print("custom named params","-"*10)
- for k,v in dictionaryParams.items():
- print(str(k)+":"+str(v))
- function2("arg1",
- "custom param1",
- "custom param2",
- "custom param3",
- param3="arg3",
- param2="arg2",
- customNamedParam1 = "val1",
- customNamedParam2 = "val2"
- )
- # Output
- #
- #param1: arg1
- #param2: arg2
- #param3: arg3
- #custom tuple params ----------
- #custom param1,
- #custom param2,
- #custom param3,
- #custom named params ----------
- #customNamedParam2:val2
- #customNamedParam1:val1
- tupleCustomArgs = ("custom param1", "custom param2", "custom param3")
- dictCustomNamedArgs = {"customNamedParam1":"val1", "customNamedParam2":"val2"}
- function2("arg1",
- *tupleCustomArgs, #note *
- param3="arg3",
- param2="arg2",
- **dictCustomNamedArgs #note **
- )
- function2("arg1",
- tupleCustomArgs, #omitting *
- param3="arg3",
- param2="arg2",
- **dictCustomNamedArgs
- )
- param1: arg1
- param2: arg2
- param3: arg3
- custom tuple params ----------
- ('custom param1', 'custom param2', 'custom param3'),
- custom named params ----------
- customNamedParam2:val2
- customNamedParam1:val1
- function2("arg1",
- *tupleCustomArgs,
- param3="arg3",
- param2="arg2",
- dictCustomNamedArgs #omitting **
- )
- dictCustomNamedArgs
- ^
- SyntaxError: non-keyword arg after keyword arg
- def print_wrap(arg1, *args, **kwargs):
- print(arg1)
- print(args)
- print(kwargs)
- print(arg1, *args, **kwargs)
- >>> print_wrap('one', 'two', 'three', end='blah', sep='--')
- one
- ('two', 'three')
- {'end': 'blah', 'sep': '--'}
- one--two--threeblah
- >>> print_wrap('blah', dead_arg='anything')
- TypeError: 'dead_arg' is an invalid keyword argument for this function
- #! /usr/bin/env python
- #
- def g( **kwargs) :
- print ( "In g ready to print kwargs" )
- print kwargs
- print ( "in g, calling f")
- f ( **kwargs )
- print ( "In g, after returning from f")
- def f( **kwargs ) :
- print ( "in f, printing kwargs")
- print ( kwargs )
- print ( "In f, after printing kwargs")
- g( a="red", b=5, c="Nassau")
- g( q="purple", w="W", c="Charlie", d=[4, 3, 6] )
- $ python kwargs_demo.py
- In g ready to print kwargs
- {'a': 'red', 'c': 'Nassau', 'b': 5}
- in g, calling f
- in f, printing kwargs
- {'a': 'red', 'c': 'Nassau', 'b': 5}
- In f, after printing kwargs
- In g, after returning from f
- In g ready to print kwargs
- {'q': 'purple', 'c': 'Charlie', 'd': [4, 3, 6], 'w': 'W'}
- in g, calling f
- in f, printing kwargs
- {'q': 'purple', 'c': 'Charlie', 'd': [4, 3, 6], 'w': 'W'}
- In f, after printing kwargs
- In g, after returning from f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement