473,561 Members | 3,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 misunderstandin g what is going on
here?
Source code:

<<<oddmodule.py >>>

print("Importin g oddmodule")
OddVariable = ["not", "initialize d"]

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

import oddmodule
import testmodule1
import testmodule2

print("Initiali zing OddVariable")

oddmodule.OddVa riable = ["some", "list"]

print()
testmodule2.DoT est()
print()
testmodule1.DoT est()
<<<testmodule1. py>>>

from oddmodule import OddVariable, OddFunction

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

import oddmodule

def DoTest():
print("STARTING testmodule2.DoT est")
print(" OddVariable from testmodule2:", oddmodule.OddVa riable)
oddmodule.OddFu nction()
print("FINISHED testmodule2.DoT est")

OUTPUT:

Importing oddmodule
Initializing OddVariable

STARTING testmodule2.DoT est
OddVariable from testmodule2: ['some', 'list']
OddVariable from oddmodule.OddFu nction: ['some', 'list']
FINISHED testmodule2.DoT est

STARTING testmodule1.DoT est
OddVariable from testmodule1: ['not', 'initialized'] !!! OLD
VALUE !!!
OddVariable from oddmodule.OddFu nction: ['some', 'list']
FINISHED testmodule1.DoT est
Many thanks,

Roman
Aug 23 '08 #1
3 1791
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
2271
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 variable I need to use in the equations are not read and the equation's result is erroneous. So if I have a variable to which a value was assigned in the...
4
2806
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 (Good Design Decisions apart), there's a performance impact in importing the same module in two different files. For example, with the following: ...
6
1513
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 time, and the time before, and the time before that, when this damn test script of Python's had me screaming at the screen... I don't seem to be...
4
8750
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 == 'optimized' from myModule_op import *
12
2364
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 some cases there is a further complication: module importing through an indirect mechanism, like: exec "from " + xxx + " import *". A part the fact...
4
2654
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 piece which holds the programs reference: globals, functions, classes, modules etc. The objects of this file (functions and classes) are directly...
4
2191
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 re-read Chapter 16 (Module Basics) in Lutz and Ascher's "Learning Python" but it's not working for me. In one module (the "source"),...
7
1592
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 compiled and executed? Only once (the first time the PVM locates, compiles and executes the sys module)? or once for each module importing sys?...
1
1150
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 know of __import__(), the imp module, and exec. For each of these, is there a way to make them work "just like" the normal import call? Thanks,...
0
7639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7855
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8074
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7926
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5465
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3594
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.