Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Surprising difference in behavior between "import blah" and "fromblah import thing"

Reply
Thread Tools

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

 
 
Eric Hanchrow
Guest
Posts: n/a
 
      05-08-2008
(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.

 
Reply With Quote
 
 
 
 
Arnaud Delobelle
Guest
Posts: n/a
 
      05-09-2008
Eric Hanchrow <(E-Mail Removed)> writes:

> (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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Surprising difference in behavior between "import blah" and"from blah import thing" J. Cliff Dyer Python 2 05-08-2008 09:17 PM
Mutable member variables -- surprising behavior Adam Bender Ruby 5 04-14-2008 06:07 PM
email.Message.get_payload() surprising behavior lrotger Python 0 07-03-2006 03:23 PM
Surprising Regexp Behavior James Edward Gray II Ruby 12 09-14-2005 02:32 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments