473,386 Members | 1,699 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,386 software developers and data experts.

Surprising difference in behavior between "import blah" and "fromblah import thing"

(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.)

This seems so basic that I'm surprised that I didn't find anything
about it in the FAQ. (Yes, I am fairly new to Python.)

Here are three tiny files:

==== mut.py ====

import system
from system import thing

def doit():
print " thing is", thing

def do_it_slightly_differently():
print "system.thing is", system.thing

==== system.py ====
thing = "I am the original thing!!"

==== test.py ====
import mut
mut.doit()
mut.do_it_slightly_differently()
import system

system.thing = "The new improved thing"
mut.doit()
mut.do_it_slightly_differently()

When I run "python test.py", I see

thing is I am the original thing!!
system.thing is I am the original thing!!
thing is I am the original thing!!
system.thing is The new improved thing

What surprises me is that the assignment to "system.thing" in test.py
only seems to affect the use of "system.thing" in mut.py, and not
affect the use of just plain "thing" in that same file. I would have
expected my assignment to have affected both, or perhaps neither.

I have no idea why these two differ. Can someone explain?
--
Rarely do we find men who willingly engage in hard, solid
thinking. There is an almost universal quest for easy answers
and half-baked solutions. Nothing pains some people more
than having to think.
-- Martin Luther King, Jr.
from "Strength to Love," 1963.

Jun 27 '08 #1
1 1939
Eric Hanchrow <of****@blarg.netwrites:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.)

This seems so basic that I'm surprised that I didn't find anything
about it in the FAQ. (Yes, I am fairly new to Python.)

Here are three tiny files:

==== mut.py ====

import system
from system import thing

def doit():
print " thing is", thing

def do_it_slightly_differently():
print "system.thing is", system.thing

==== system.py ====
thing = "I am the original thing!!"

==== test.py ====
import mut
mut.doit()
mut.do_it_slightly_differently()
import system

system.thing = "The new improved thing"
mut.doit()
mut.do_it_slightly_differently()

When I run "python test.py", I see

thing is I am the original thing!!
system.thing is I am the original thing!!
thing is I am the original thing!!
system.thing is The new improved thing

What surprises me is that the assignment to "system.thing" in test.py
only seems to affect the use of "system.thing" in mut.py, and not
affect the use of just plain "thing" in that same file. I would have
expected my assignment to have affected both, or perhaps neither.

I have no idea why these two differ. Can someone explain?
--
Rarely do we find men who willingly engage in hard, solid
thinking. There is an almost universal quest for easy answers
and half-baked solutions. Nothing pains some people more
than having to think.
-- Martin Luther King, Jr.
from "Strength to Love," 1963.
import system
from system import thing

is the same as:

import system
thing = system.thing

Do you expect system.thing to be rebound when you rebind thing?

HTH

--
Arnaud

Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
1
by: Steve Juranich | last post by:
First of all, just let me say that I'm aware of the "-v" switch for Python, and I don't want anything nearly that verbose. I often long for the following behavior from Python when I'm running...
1
by: Xiao Jianfeng | last post by:
Hello, In pymol I can use "from chempy import Atom" but "import chempy.Atom" doesn't work. It says,"ImportError: No module named Atom". What is going wrong ? Thanks
2
by: mithrond | last post by:
i can't use "from __future__ import ..." statement import two statesments in the same time. for the simple code: from __future__ import with_statement, division with file('url.txt','r') as f:...
3
by: Jim Hill | last post by:
Well, I've found about a hundred thousand web pages where people have had the same problem I have but nary a page with a solution that works for me. I want to do a simple embed, so I've followed...
1
by: David Hirschfield | last post by:
I had a situation recently that required I manually load python bytecode from a .pyc file on disk. So, for the most part, I took code from imputil.py which loads the .pyc data via the marshal...
1
by: smalltalk | last post by:
I have three files names t1.py,t2.py,t3.py in e:\test\dir1,of course dir2 is exsit the content of t1.py as follow: t1.py import os print 'this is t1.py' os.chdir('..\\dir2') the content of...
3
by: J. Cliff Dyer | last post by:
On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote: It's the same reason as this: 5 5 6 5 Python "variables" are just names that point at objects. When you
1
by: Pierre Dagenais | last post by:
from Tkinter import * win = Tk() If I type those two lines at the command prompt (in WindowsXP) I get a new window on my screen. Yet if I copy those lines in a file called test.py and then...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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,...

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.