473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1775
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.