471,325 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Imported globals?


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.



Jul 27 '07 #1
0 731

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Thomas Mlynarczyk | last post: by
reply views Thread by Berteun Damman | last post: by
5 posts views Thread by James Tauber | last post: by
reply views Thread by Kevin Fernandes | last post: by
3 posts views Thread by Zachary Pincus | last post: by
10 posts views Thread by Michael Abbott | last post: by
1 post views Thread by Jeff Dyke | last post: by
reply views Thread by Jeff Dyke | last post: by
4 posts views Thread by RgeeK | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.