[Python-Dev] Explicit Lexical Scoping (pre-PEP?) (original) (raw)

Andrew Koenig ark at acm.org
Tue Jul 4 16:16:59 CEST 2006


Borrowing from Perl, the keyword 'my' is used to declare an explicitly scoped variable:

def f1(): my x = 1 def f2(): x = 2 # Does not create a new x In the above example, the statement 'my x = 1' declares that the scope of the variable 'x' is the outer function f1. Any assignment to x will modify the existing x, rather than creating a new definition.

-1, for this reason:

def f()
    x = 2		# Does this create a local variable?

Today, the answer is yes. Under this proposal, you can't answer the question without inspecting the entire context in which f is defined.

For that reason, I would much rather have the first assignment in a block say explicitly whether it is intended to create a local variable:

def f1():
   x = 1
   def f2():
      global x
      x = 2    # Does not create a new x

This might even be abbreviated:

def f1():
   x = 1
   def f2():
      global x = 2   # Equivalent to the last example above


More information about the Python-Dev mailing list