question od default args
Trent Mick
trentm at ActiveState.com
Wed Jun 26 13:55:17 EDT 2002
On 26-Jun-2002 Gonçalo Rodrigues wrote:
> Hi,
>
> 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.
You could try something like this:
>>> class _NoArgSpecified:
... pass
...
>>> def foo(arg=_NoArgSpecified):
... if arg is _NoArgSpecified:
... print "no arg specified"
... else:
... print "arg:", repr(arg)
...
>>> foo()
no arg specified
>>> foo(None)
arg: None
>>> foo(1)
arg: 1
Mind you, the user *can* still cheat, but you shouldn't have to worry
about that.
>>> foo(_NoArgSpecified)
no arg specified
>>>
Trent
--
Trent Mick
TrentM at ActiveState.com
More information about the Python-list
mailing list