Imported globals?

star.public at gmail.com star.public at gmail.com
Fri Jul 27 21:01:05 CEST 2007


On Jul 27, 1:30 pm, Valentina Vaneeva <fat... at tpu.ru> wrote:

> Thank you, Gary, but I still have one question. What happens in the
> second case? If I add a call to change_value() to module_a, the value
> in module_b is imported changed. Why? What exactly does the import
> statement import in my example?

Because then by the time you import the thing, that action has been
carried out. Consider:

from module_a import change_value
change_value()
from module a import value
print value

It's not significantly different from this:

-----
class NotaModule(object):
  pass

nota = NotaModule()
nota.a = 1

def change_it():
  global nota # not even sure you need this
  nota.a = 2

a = nota.a
change_it()
print a, nota.a
------

Modules and (singleton) objects are rather similar from the outside,
afaik.

Also consider that this is yet another case where if value or A was a
list and you appended to it, both prints would show the change,
mutable objects, etc.

--
Tired Weaver




More information about the Python-list mailing list