[Python-ideas] __missing__ object/keyword

Bruce Leban bruce at leapyear.org
Sat Nov 8 06:37:06 CET 2008


The problem with using a value like None as some have proposed is that None
may be a valid parameter value. Thus the only safe value is a special value
like a throwaway object. There are numerous problems with this including
that it's not safe: I might not check for the missing value in some place
and end up passing the throwaway object to another function which then
passes it as one of the optional values.
There *is* one guaranteed way of ensuring that I can't use a variable's
value: leaving it unbound. That is, to support this, we would add a new
syntax something like what George proposed with slightly different
semantics:

  def foo(x, y=__unbound__):

where if y is omitted from the call, then y is unbound. This is slightly
different than what would happen if y were left out of the parameter list as
in that case, y could reference a global of the same name. In this case, y
can only reference an unbound local.

No other changes are required to use this. If I don't check whether or not y
is bound, then I'll get a NameError when I try to use it, just as with any
other unbound variable.

If this proposal were to be seriously considered, there are of course
alternative syntaxes that could be considered, like using missing,
__missing__ or def foo(x, ?y) but that's getting ahead.

I agree it would be nice to have a way to check whether a variable is
unbound without writing a multi-line try/except, but I think that can be
discussed as a different issue.

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20081107/ecd3a36c/attachment.html>


More information about the Python-ideas mailing list