question od default args

Fredrik Lundh fredrik at pythonware.com
Wed Jun 26 14:17:28 EDT 2002


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:
            ...

or, quite ugly:

    def myfunc(*args):
        if not args:
            print "no argument"
        else:
            (arg,) = args
            ...

or, preferred:

    come up with a better design; good designs allow people to
    explicitly pass in a "use the default value" argument value...

</F>





More information about the Python-list mailing list