what is happening here?

Andrew Wilkinson ajw140NO at SPAMyork.ac.uk
Wed Apr 7 06:51:41 EDT 2004


dan miller (moderator, s.p.d) wrote:
>  >>> from test import *

Here you create a variable glob that is a reference to the one in your test
module.

>  >>> glob
> 1

This returns the value of glob in the test module

>  >>> getglob()
> 1

Here you're calling getglob, which is in the test module.

>  >>> glob=2

Here's where your problem lies... You're creating a new variable glob that
*replaces* the old variable. The variable that is also called glob in the
test module is unaffected.

>  >>> glob
> 2

Now you're refering to the newly create variable, hence the value 2.

>  >>> getglob()
> 1

Here you're calling getglob, which is in the test module and hence it uses
the glob variable in test - which has the value 1.

>  >>> setglob(3)
>  >>> getglob()
> 3

Same with these two, they're both in test - and so use the variable glob in
test.

>  >>> glob
> 2

This still refers to glob in your local namespace, which still has the value
2.

Hope this is of some help,
Andrew



More information about the Python-list mailing list