Custom Search

Friday, April 2, 2010

Python Function arguments

Python Function arguments:


Function arguments: variable (*args)

===========================

• List of any number of arguments

• Useful when unknown number of arguments needed

• The argument values collected into a tuple

– Called 'args', by convention

– The ’*’ is the magical part

def unspec_args(a, *args): # name 'args' is a convention

return "a=%s, others=%s" % (a, args)

print unspec_args('bla', 'qwe', 23, False)

output:

a=bla, others=('qwe', 23, False)

Function arguments: keywords (**kwargs)

===============================

• Keyword/value arguments

• The argument values collected into a dictionary

– Called 'kwargs', by convention

– The ’**’ is the magical part

– First attempts to match existing argument names

def keyword_args(a, b='bla', **kwargs):

return "a=%s, b=%s, kwargs=%s" % (a, b, str(kwargs))

print keyword_args('stuff', c='call')

print keyword_args('stuff', c='call', b='apa')

print keyword_args(c='call', d=12, a='gr')

output:

a=stuff, b=bla, kwargs={'c': 'call'}

a=stuff, b=apa, kwargs={'c': 'call'}

a=gr, b=bla, kwargs={'c': 'call', 'd': 12}

Function arguments: default values

=========================

• Arguments may have default values

• When argument not given in a call, default value is used

• If no default value, and not given when called: bombs

• Use explicit names to override argument order

def default_args(a, b='bar', c=13):

return "a=%s, b=%s, c=%s" % (a, b, c)

print default_args('apa') # uses all default values

print default_args('s', b='py') # overrides one default value

print default_args(c=26,

output:

a='apa') # override argument order

a=apa, b=bar, c=13

a=s, b=py, c=13

a=apa, b=bar, c=26

Function arguments: explicit type checking

===============================

• Use the 'assert' statement

• Checks that its Boolean expression is True, else bombs

• Can be used for sanity checks anywhere in code

• Optional explanatory message (or data)

def fixed_args(a, c, b):

assert type(a) == type(1), "'a' must be an integer"

return "a=%s, b=%s, c=%s" % (a, b, c)

print fixed_args('a', 1.2, [2, 1])

output:

Traceback (most recent call last):

File "C:\Python tests\t15.py", line 8, in toplevelprint

fixed_args('a', 1.2, [2, 1])

File "C:\Python tests\t15.py", line 5, in fixed_args

assert type(a) == type(1), "'a' must be an integer"

AssertionError: 'a' must be an integer

Function without 'return': value None

===========================

• A function does not have to use the 'return' statement

• If not, then same as a 'procedure' in other languages

• Actually returns a value anyway: 'None'

• A 'return' without value is OK: returns 'None'

• 'None' is a special value meaning 'nothing'

– Useful in many contexts

– Particularly in object-oriented programming (more later)

def concat_strings(a, b):

str_type = type('') # save a type value!

if type(a) == str_type and type(b) == str_type:

return a + ' ' + b

print 'strings:', concat_strings('first', 'second')

print 'integers:', concat_strings(1, 2)

output:

strings: first second

integers: None


**************************


No comments:

Post a Comment