question od default args
Mark Jackson
mjackson at wrc.xerox.com
Thu Jun 27 09:35:21 EDT 2002
"Fredrik Lundh" <fredrik at pythonware.com> writes:
> Gonçalo Rodrigues wrote:
>
> > When I want default args I usually do
> >
> > def (arg = None):
> > ...
> >
> > But what if I want to make None a reasonable argument too? That is, I
> > want to know if the user has in fact passed an argument, and if not to
> > do something special, with None (or whatever object) being a reasonable
> > arg to be passed.
>
> hmm. wasn't this just discussed in some thread on a
> newsgroup near you?
>
> three alternatives:
>
> __UNDEF__ = [] # guaranteed to have a unique id
>
> def myfunc(arg=__UNDEF__):
> if arg is __UNDEF__:
> print "no argument"
> else:
> ...
More quickly:
def myfunc(arg=[]):
if arg is myfunc.func_defaults[0]:
print "no argument"
else:
...
--
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
Until we test our beliefs, we can't say for sure if we have
leeches or we have aspirin. - David Faigman
More information about the Python-list
mailing list