[Python-ideas] __missing__ object/keyword
Greg Ewing
greg.ewing at canterbury.ac.nz
Sat Nov 8 23:06:11 CET 2008
Bruce Leban wrote:
> However, there is an implementation complication due to the way variable
> scoping works in Python: if a variable is left unbound in the function
> locals, then references to that variable name would pick up the global
> if it exists
No, it wouldn't:
Python 2.3 (#1, Aug 5 2003, 15:52:30)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... print x
... x = 0
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'x' referenced before assignment
Python already has the machinery to make this work. All
that's needed is a way to leave parameters unbound, and
a syntax for asking "Would I get a NameError if I were to
refer to this name now?"
Perhaps something like
def f(x?):
if x?:
...
--
Greg
More information about the Python-ideas
mailing list