473,472 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Re: Surprising difference in behavior between "import blah" and"from blah import thing"

On Thu, 2008-05-08 at 12:00 -0700, Eric Hanchrow wrote:
(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?
It's the same reason as this:
>>x=5
y=x
x
5
>>y
5
>>x=6
x
6
>>y
5
>>>
Python "variables" are just names that point at objects. When you
import, the imported module gets bound to a name. When you use the an
assignment statement, you bind a new object to the name, but other names
bound to the object are not affected.

So in your code, when you say
>>system.thing = "The new improved thing"
You rebind the name system.thing, but the object it was originally bound
to is unaffected. Strings are immutable, so you couldn't change that
object if you tried. thing is still bound to the original object.
Another feature of this behavior is as follows:
>>x=5
x is y
True
>>x=5
y=x
x is y
True
>>x=6
x is y
False
>>>
The "is" operator tells you that the two objects are the same object.
When you reassign one, it no longer points to the same object as the
other. You would get the same behaviour in your code comparing
system.thing and thing.

Cheers,
Cliff
Jun 27 '08 #1
3 1647
Ah. So from the point of view of mut.py, "thing" and "system.thing"
are separate, unrelated variables; the former of which is initialized
from the latter when mut says "from system import thing". Thanks.
Jun 27 '08 #2
:'(
I'm confused

On Fri, May 9, 2008 at 12:03 AM, offby1 <er***********@gmail.comwrote:
Ah. So from the point of view of mut.py, "thing" and "system.thing"
are separate, unrelated variables; the former of which is initialized
from the latter when mut says "from system import thing". Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


--
http://www.badmuthahubbard.com
Jun 27 '08 #3
offby1 <er***********@gmail.comwrites:
Ah. So from the point of view of mut.py, "thing" and "system.thing"
are separate, unrelated variables;
No. Thinking of them as "variables" (with all the non-Python
terminological baggage that implies) will only exacerbate the
confusion.

"thing" and "system.thing" are separate *names*, that can be
independently bound to different objects or the same object.
the former of which is initialized from the latter when mut says
"from system import thing".
Rather, the name "thing" is bound to the same object as the name
"system.thing" at the time "from system import thing" is executed.
What happens to the name "system.thing" afterward has no effect on the
name "thing".

--
\ "Listen: we are here on Earth to fart around. Don't let anybody |
`\ tell you otherwise." -- _Timequake_, Kurt Vonnegut |
_o__) |
Ben Finney
Jun 27 '08 #4

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

Similar topics

2
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
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
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
7
by: per9000 | last post by:
Dear Black Knight, I have no quarrel with you sir Knight, but I must import your parents. SHORT VERSION: I tried three variants of "from ../brave.py import sir_robin", one works. I want...
14
by: Paulo da Silva | last post by:
Hi! If I have two files .py such as m.py from c import * ... x=c() ... os.any_method ...
6
by: =?Utf-8?B?SmVmZg==?= | last post by:
I thought this would already be covered here, but my search turned up nothing. In VS2005, if I use "String" to define a new variable/class, it colors it in the Aqua color as it does other...
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: Eric Hanchrow | last post by:
(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...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
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.