PI. Keyword arguments

Run Settings
LanguagePython
Language Version
Run Command
# To explain next part of material lets split function's arguments into two # groups of definition phase. # # First group of definition (D1) is an arguments that have no default values. # Here's ``voltage`` goes to this group. # # Second group of definition (D2) is an arguments that have default values. # Here's ``state``, ``action`` and ``type`` gors to this group. # # During definition both groups should stay seperately and shouldn't be mixed. # Order of their appearence of elements of this groups goes in order of names of # groups. First definition group arguments (without default values) and then # Second definition group (with default values). # # Any of this groups could be absent. def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): print "-- This parrot wouldn't", action print "If you put", voltage, "volts through it." print "-- Lovely plumage, the", type print "-- It's a", state, "!" print # Now lets define two call groups orthogonal to definition groups. # # Positional arguments (or unnamed) (PA) is an arguments what represents only by # value, they should be defined in the order of their definition. # # Keyword (or named) is an arguments (KW) what represents by their definiton name and # value, like: voltage=10000, action="sniff", etc. They could follow in any order # but only after group of positional arguments. # # And now, the most interesting part. Here's some ground rules of this game: # 1. All arguments of D1 should be present # 2. All PA always goes in the order of their definition, they could be D1 or D2 # 3. All KW always goes after PA, if last are present # 4. Order of KW does not metters # # And now some examples to explain how it works: parrot(1000) # 1 pos argument parrot(voltage=1000) # 1 kwd argument parrot(voltage=1000000, action='VOOOOOM') # 2 kwd arguments parrot(action='VOOOOOM', voltage=1000000) # 2 kwd arguments parrot('a million', 'bereft of life', 'jump') # 3 pos arguments parrot('a thousand', state='pushing up the daisies') # 1 pos, 1 kwd
Editor Settings
Theme
Key bindings
Full width
Lines