On 7 Mai 2005, [EMAIL PROTECTED] wrote:
> I came across a rather odd issue with scoping. Can someone explain why
> testa and testc works, but not testb. I am running under python 2.4.1 on
[...]
> x = 5
>
> def testa(astr):
> print astr, x
>
> testa(22)
>
> def testb(astr):
> x = x - 1
> print astr, x
>
> testb(22)
>
> def testc(astr):
> print astr, x-1
>
> testc(22)
In testb you make an assignment to `x'. So `x' is no longer the global
`x' but a local `x'. But since is has no local value before the
assignment you get an error. You could tell Python to use the global
value of `x'.
def testb(astr):
global x
x = x - 1
print astr, x
Karl
--
Please do *not* send copies of replies to me.
I read the list
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor