468,317 Members | 1,521 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,317 developers. It's quick & easy.

Odd behaviour on importing variable from module

Hi,

I've found the following behaviour on importing a variable from a
module somewhat odd. The behaviour is identical in Python 2.5 and
3.0b2.

In summary, here's what happens. I have a module, oddmodule.py
(below), that defines a variable, OddVariable, by assigning a value A
to it. The file I execute, mainfile.py, imports and re-binds
OddVariable to a value B. I have two test modules which import the
OddVariable in two different ways, one via "import oddmodule" and
another via "from oddmodule import OddVariable".

The weird behaviour is that by importing using the former syntax, I
can see OddVariable bound to B (as expected), but the latter syntax
sees it bound A.

Is this the intended behaviour? Am I misunderstanding what is going on
here?
Source code:

<<<oddmodule.py>>>

print("Importing oddmodule")
OddVariable = ["not", "initialized"]

def OddFunction():
print(" OddVariable from oddmodule.OddFunction:", OddVariable)
<<<mainfile.py>>>

import oddmodule
import testmodule1
import testmodule2

print("Initializing OddVariable")

oddmodule.OddVariable = ["some", "list"]

print()
testmodule2.DoTest()
print()
testmodule1.DoTest()
<<<testmodule1.py>>>

from oddmodule import OddVariable, OddFunction

def DoTest():
print("STARTING testmodule1.DoTest")
print(" OddVariable from testmodule1:", OddVariable)
OddFunction()
print("FINISHED testmodule1.DoTest")
<<<testmodule2.py>>>

import oddmodule

def DoTest():
print("STARTING testmodule2.DoTest")
print(" OddVariable from testmodule2:", oddmodule.OddVariable)
oddmodule.OddFunction()
print("FINISHED testmodule2.DoTest")

OUTPUT:

Importing oddmodule
Initializing OddVariable

STARTING testmodule2.DoTest
OddVariable from testmodule2: ['some', 'list']
OddVariable from oddmodule.OddFunction: ['some', 'list']
FINISHED testmodule2.DoTest

STARTING testmodule1.DoTest
OddVariable from testmodule1: ['not', 'initialized'] !!! OLD
VALUE !!!
OddVariable from oddmodule.OddFunction: ['some', 'list']
FINISHED testmodule1.DoTest
Many thanks,

Roman
Aug 23 '08 #1
3 1483
rs387 wrote:
I've found the following behaviour on importing a variable from a
module somewhat odd. The behaviour is identical in Python 2.5 and
3.0b2.
the construct

from oddmodule import OddVariable, OddFunction

assigns the *values* of the given module names to new variables in the
importing module's namespace. that is, you're binding new names to the
values the variables happen to have when the from-import statement is
executed. given this, the behaviour should be no more surprising than

A = "value"
B = A
A = "new value"
print B # prints "value"

reading up on how objects and names work in Python could be a good idea;
this page might help:

http://effbot.org/zone/python-objects.htm

</F>

Aug 23 '08 #2
rs387 wrote:
I've found the following behaviour on importing a variable from a
module somewhat odd. The behaviour is identical in Python 2.5 and
3.0b2.

In summary, here's what happens. I have a module, oddmodule.py
(below), that defines a variable, OddVariable, by assigning a value A
to it. The file I execute, mainfile.py, imports and re-binds
OddVariable to a value B. I have two test modules which import the
OddVariable in two different ways, one via "import oddmodule" and
another via "from oddmodule import OddVariable".

The weird behaviour is that by importing using the former syntax, I
can see OddVariable bound to B (as expected), but the latter syntax
sees it bound A.

Is this the intended behaviour?
Yes. Think of

from module import var

as a shortcut for

import module
var = module.var
del module

This is not entirely correct as 'from package import module' implicitly
imports 'module' but should explain the binding behaviour.

Peter
Aug 23 '08 #3
the construct
>
from oddmodule import OddVariable, OddFunction

assigns the *values* of the given module names to new variables in the
importing module's namespace. that is, you're binding new names to the
values the variables happen to have when the from-import statement is
executed.
Ah right, this does indeed explain what I'm seeing. For some reason I
thought "from module import variable" magically makes "variable" an
"alias", so to speak, for "module.var", which of course it does not.
Yes. Think of

from module import var

as a shortcut for

import module
var = module.var
del module
Good tip, I'll remember this. Thanks very much Fredrik, Peter!

Roman
Aug 23 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jason Charalambides | last post: by
6 posts views Thread by John J. Lee | last post: by
4 posts views Thread by Bo Peng | last post: by
12 posts views Thread by qwweeeit | last post: by
4 posts views Thread by jean-marc | last post: by
7 posts views Thread by Hussein B | last post: by
1 post views Thread by Sean Davis | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.