Odd behaviour on importing variable from module

R

rs387

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
 
F

Fredrik Lundh

rs387 said:
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>
 
P

Peter Otten

rs387 said:
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
 
R

rs387

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top