Imported globals?

V

Valentina Vaneeva

Hi,

I'm new to Python and I've found something in its interpreter that I
don't quite understand, and I don't really know how to correctly
formulate a search query. Here's the question.

If we have a file module_a.py with the following content:

| #!/usr/bin/env python
|
| value = 'initial'
|
| def change_value():
| global value
| value = 'changed'

and a file module_b.py:

| #!/usr/bin/env python
|
| from module_a import value, change_value
|
| change_value()
| print value

Then, if we run module_b.py, it will print "initial". However, if one
adds to the end of module_a.py this line:

| change_value()

The script module_b.py will print "changed".

It seems that in the first case change_value() called in module_b.py
ignores the global statement. Is it so? Why? What happens in the second
case? I really don't get it.

Thanks!

Cheers,
Valia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top