Explicit variable declaration

Terry Reedy tjreedy at udel.edu
Thu Apr 24 10:40:25 CEST 2008


"Filip Gruszczynski" <gruszczy at gmail.com> wrote in message 
news:1be78d220804231829k2c040fat80f6fe8b96e1b7cf at mail.gmail.com...
|>  If you want to just declare that name exist, but doesn't want to
| >  declare the type, why don't you just do this:

Names do not 'exist' in Python, nor do they have types.  They are bound to 
objects that have types.  Learn to program Python as Python, not one of 
those languages with a quite different model of names and values.

| >  def somefunc():
| >     nonlocal = nonlocal

Syntax error in 3.0.  Error or nonsense in 2.x

| >     local = 0 # or None or [] or an initial value
| >     #
| >     return nonlocal * local
|
| Err.. I don't quite get. How it may help me? Could you explain?

Forget the above.  The only 'declarations' in Python, 'global' and 
'nonlocal' are for the specialized purpose of *binding* names that are not 
in the local namespace of a function or nested function.  They are only 
needed because otherwise names that get bound are otherwise assumed to be 
local.  See the language ref section on function defs.

tjr






More information about the Python-list mailing list