Python-Ref > Basic usage > Functions > Creating functions with keyword arguments
 
 

<-^^->
Moduly
Knihovní funkce

Creating functions with keyword arguments

How to create a function that has optional (keyword) arguments.
As well as the builtin functions, the user defined functions may specify optional (keyword) arguments.
Keyword arguments are arguments that have a specified default value and might therefor be omitted. Values for keyword arguments may be specified either as normal arguments or in the form keyword=value. In the latter case, the arguments do not have to specified in the order in which they appear in the declaration.
Expand/Shrink
iso8859-2.2.txt [iso8859-2]
ěščřřžžýýááíů

Text v kódování iso-8859-2.
Zdroj: (func4-1.py)
  1   def remove_ascii( text, replace_by='?'):
  2       """replaces all non-ascii letters with '?', but has the option
  3       to use a different character as replacement."""
  4       ret = []
  5       for char in text:
  6           if ord( char) < 128:
  7               ret.append( char)
  8           else:
  9               ret.append( replace_by)
 10       return ''.join( ret)
 11   
 12   f = file( "iso8859-2.2.txt", "r")
 13   text = f.read()
 14   f.close()
 15   
 16   print remove_ascii( text)
 17   print remove_ascii( text, replace_by="!")
 18   print remove_ascii( text, "*") # you can omit the name of the argument
stdout:
?????????????

Text v k?dov?n? iso-8859-2.

!!!!!!!!!!!!!

Text v k!dov!n! iso-8859-2.

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

Text v k*dov*n* iso-8859-2.

Doba běhu: 21.3 ms
Expand/Shrink
iso8859-2.2.txt [iso8859-2]
ěščřřžžýýááíů

Text v kódování iso-8859-2.
Zdroj: (func4-2.py)
  1   def remove_characters_by_code( text, below=0, above=127, replace_by='?'):
  2       """replaces all letters with ord<below or ord>above with replace_by,
  3       by default replaces all non-ascii characters by '?'."""
  4       ret = []
  5       for char in text:
  6           if below <= ord( char) <= above:
  7               ret.append( char)
  8           else:
  9               ret.append( replace_by)
 10       return ''.join( ret)
 11   
 12   f = file( "iso8859-2.2.txt", "r")
 13   text = f.read()
 14   f.close()
 15   
 16   print remove_characters_by_code( text)
 17   print "---"
 18   print remove_characters_by_code( text, replace_by="!")
 19   print "---"
 20   print remove_characters_by_code( text, below=50, replace_by="*") # position does not matter
 21   print "---"
 22   print remove_characters_by_code( text, replace_by="*", below=50) # position does not matter
 23   print "---"
 24   print remove_characters_by_code( text, replace_by="*", below=50, above=110)
 25   print "---"
 26   print remove_characters_by_code( text, 50, 110, "*") # position matters
 27   print "---"
 28   print remove_characters_by_code( text, 50, "*", above=100) # position matters
 29   print "---"
stdout:
?????????????

Text v k?dov?n? iso-8859-2.

---
!!!!!!!!!!!!!

Text v k!dov!n! iso-8859-2.

---
***************Text*v*k*dov*n**iso*8859*2**
---
***************Text*v*k*dov*n**iso*8859*2**
---
***************Te*****k*d***n**i***8859*2**
---
***************Te*****k*d***n**i***8859*2**
---
stderr:
Traceback (most recent call last):
  File "func4-2.py", line 28, in <module>
    print remove_characters_by_code( text, 50, "*", above=100) # position matters
TypeError: remove_characters_by_code() got multiple values for keyword argument 'above'
Doba běhu: 21.2 ms
Expand/Shrink
iso8859-2.2.txt [iso8859-2]
ěščřřžžýýááíů

Text v kódování iso-8859-2.
Zdroj: (func4-3.py)
  1   def remove_characters_by_code( text, below=0, above=127, replace_by='?'):
  2       """replaces all letters with ord<below or ord>above with replace_by,
  3       by default replaces all non-ascii characters by '?'."""
  4       ret = []
  5       for char in text:
  6           if below <= ord( char) <= above:
  7               ret.append( char)
  8           else:
  9               ret.append( replace_by)
 10       return ''.join( ret)
 11   
 12   f = file( "iso8859-2.2.txt", "r")
 13   text = f.read()
 14   f.close()
 15   
 16   print remove_characters_by_code( text, replace_by="*", below=50, above=110)
 17   print remove_characters_by_code( text, 50, 110, "*") # position matters
 18   print remove_characters_by_code( text, 50, "*", above=100) # position matters
stdout:
***************Te*****k*d***n**i***8859*2**
***************Te*****k*d***n**i***8859*2**
stderr:
Traceback (most recent call last):
  File "func4-3.py", line 18, in <module>
    print remove_characters_by_code( text, 50, "*", above=100) # position matters
TypeError: remove_characters_by_code() got multiple values for keyword argument 'above'
Doba běhu: 22.1 ms
Note, however, that all non-keyword arguments must appear before any keyword arguments.
Expand/Shrink
iso8859-2.2.txt [iso8859-2]
ěščřřžžýýááíů

Text v kódování iso-8859-2.
Zdroj: (func4-4.py)
  1   def remove_characters_by_code( text, below=0, above=127, replace_by='?'):
  2       """replaces all letters with ord<below or ord>above with replace_by,
  3       by default replaces all non-ascii characters by '?'."""
  4       ret = []
  5       for char in text:
  6           if below <= ord( char) <= above:
  7               ret.append( char)
  8           else:
  9               ret.append( replace_by)
 10       return ''.join( ret)
 11   
 12   f = file( "iso8859-2.2.txt", "r")
 13   text = f.read()
 14   f.close()
 15   
 16   print remove_characters_by_code( text, replace_by="*", below=50, above=110)
 17   print remove_characters_by_code( text, 50, 110, "*") # position matters
 18   print remove_characters_by_code( text, 50, above=100, "*") # non-keyword cannot be before keyword
stderr:
  File "func4-4.py", line 18
    print remove_characters_by_code( text, 50, above=100, "*") # non-keyword cannot be before keyword
SyntaxError: non-keyword arg after keyword arg
Doba běhu: 19.6 ms
Therefore it is wise to use either one or the other approach, but not to mix them. I would recommend using the keyword=value combination as it is less prone to errors.
A more free-style approach to optional arguments may be found in the example Creating functions with variable number of arguments.