Scope rule pecularities
Yermat
loic at fejoz.net
Thu May 6 10:40:31 EDT 2004
Antoon Pardon wrote:
> [...]
> a1 = Int(14)
> a2 = Int(15)
> b = Int(23)
>
> print a1, a2
>
> def foo():
>
> a1 += b
> a2.__iadd__(b)
>
> foo()
>
> print a1, a2
>
>
> Now the a1 += b line doesn't work, it produces the following error:
>
> UnboundLocalError: local variable 'a1' referenced before assignment.
>
> The a2.__iadd__(b) line however works without trouble.
>
>
> Now I think I understand what is causing this, but I think this
> kind of thing shouldn't happen. If a += b is just syntatic sugar
> for a.__iadd__(b) then the first should be acceptable where the
> second is acceptable.
>
Here we are again !
Do you mind to try :
def foo():
global a1, a2
a1 += b
a2.__iadd__(b)
Note also that the second line were not executed so you can't know if it
were working...
--
Yermat
More information about the Python-list
mailing list