Imported globals?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Valentina Vaneeva

    #1

    Imported globals?

    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

Working...