472,354 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 1664
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason Charalambides | last post by:
Is there a way I can import a given value to a variable from the main form to a module? I wanted to have some routines that are repeated set in a module subroutine. However, the value of a...
4
by: Andrew James | last post by:
Hi, I've been looking around on Google for the answer to this question, and it's beginning to really bug me. I'm making some design decisions for some code I'm writing, and I'm wondering whether...
6
by: John J. Lee | last post by:
I'm tearing my hair out at what seems like weird import behaviour I'm getting from Python's stdlib test script, regrtest.py (not for the first time: seem to have forgotten the resolution from last...
4
by: Bo Peng | last post by:
Dear list, What I would like to do is something like: In myModule.py ( a wrapper module for different versions of the module), if lib == 'standard': from myModule_std import * elsif lib ==...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
4
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center...
4
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
7
by: Hussein B | last post by:
Hey, Suppose I have a Python application consists of many modules (lets say it is a Django application). If all the modules files are importing sys module, how many times the sys module will be...
1
by: Sean Davis | last post by:
What is the "best practice" for importing an arbitrary module given that the name is stored in a variable? The context is a simple web application with URL dispatching to a module and function. I...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.