Automatic binding of **kwargs to variables

Alex Martelli aleaxit at yahoo.com
Sun Oct 30 10:35:13 EST 2005


bonono at gmail.com <bonono at gmail.com> wrote:

> Don't know about this particular case but sometimes, I don't want to
> have a default argument value. That is, argument not there is different
> from argument = None. Though in general, I prefer the None as special
> meaning coding style. But even python's builtin function prefers to
> distinguish between "not there" and None.

the canonical idiom when you need such distinction is:

_not_there = object()
def foo(bar=_not_there, baz=_not_there, bap=_not_there):
    if bar is _not_there: ...

Other unique objects can be substituted for the 'sentinel', but I prefer
an empty "object()" because it has no other possible meaning except that
of a distinguishable, identifiable sentinel.  IOW, you could set the
_not_there name to [] or {} or many other things, but that could be
slightly confusing for the reader (since the other things might have
other meanings and purposes) while 'object()' shouldn't be.

At any rate, the complicated code I was commenting on did treat absent
expected arguments exactly the same way as arguments passed as None,
which is also a frequent use case, so this further level of refinement
was apparently not warranted ("use only as much power as you need" is a
programming principle that tends to promote simplicity, and therefore,
in many cases, is well worth adhering to).


Alex



More information about the Python-list mailing list