Accessing variable from a function within a function

Stefan Bellon sbellon at sbellon.de
Sun Jun 24 15:04:11 EDT 2007


On Sun, 24 Jun, 7stud wrote:

> ef outer():
>     a = 10
>     def inner():
>         print a
> 
>     return inner
> 
> 
> f = outer()
> f()
> 
> --output:--
> 10
> 

>>> def outer():
...     a = 10
...     def inner():
...         a = a + 1
...         print a
...     return inner
... 
>>> f=outer()
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in inner
UnboundLocalError: local variable 'a' referenced before assignment

-- 
Stefan Bellon



More information about the Python-list mailing list