[Pythonmac-SIG] importing namespaces/modules

Piet van Oostrum piet at cs.uu.nl
Fri Jan 19 10:37:00 CET 2007


I should add another difference in the behaviour of variables import in one
or the other way. This can lead to subtle differences.

If you use 'from module import x, y' (or 'from module import *') Python
makes new `bindings' of the names `x' and `y' in the importing module with
the values these names had in the imported module. However they are
independent of the original ones. So if you execute an assignment to x in
the original module, e.g. in a function that was defined there and that you
also imported, the change in x is not reflected in the imported x, and vice
versa. This can lead to unexpected behaviour.

If, however, you say 'import module' and then use module.x, then it is the
same variable, and changes in the imported module's x will be visible in
the importing module's module.x and vice versa.

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org


More information about the Pythonmac-SIG mailing list