Function arguments
Laura Creighton
lac at strakt.com
Thu Oct 18 06:36:22 EDT 2001
> Call me weird, but sometimes I need functions taking a number of
> positional arguments, some named arguments with default values, and
> other named arguments as well.
<snip>
> I was thinking of a popitem() dictionary method taking
> (optionally) 2 arguments: the name of the item to pop,
> and the default value to return if the item is not present
> in the dictionary:
>
> >>> d = {'a': 2, 'b': 3}
> >>> d.popitem('defarg', 0)
> 0
> >>> d
> {'a': 2, 'b': 3}
> >>> d.popitem('a', 100)
> 2
> >>> d
> {'b': 3}
> >>>
>
> Opinions?
I do this frequently. (Code yanked from what I happen to be working on
today).
class Field(Tkinter.Label):
def __init__(self, parent, **kw):
# these are the defaults that you will get if you haven't specified
defaults={'borderwidth': 1,
'relief' : 'groove',
'width' : 30,
'height' : 2,
'anchor' : 'w',
'text' : 'SPAM SPAM SPAM SPAM',
}
# overwrite any of the defaults with the dictionary you passed
defaults.update(kw)
Tkinter.Label.__init__(self, parent, **defaults)
and so on and so forth
Laura Creighton
More information about the Python-list
mailing list