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

Discussion in 'Python' started by Eric Hanchrow, May 8, 2008.

  1. (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.
     
    Eric Hanchrow, May 8, 2008
    #1
    1. Advertising

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

    Eric Hanchrow <> 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
     
    Arnaud Delobelle, May 9, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Cliff Dyer
    Replies:
    2
    Views:
    345
    Chuckk Hubbard
    May 8, 2008
  2. Skip Montanaro
    Replies:
    0
    Views:
    167
    Skip Montanaro
    May 30, 2013
  3. Cameron Simpson
    Replies:
    0
    Views:
    113
    Cameron Simpson
    May 31, 2013
  4. Göktuğ Kayaalp
    Replies:
    0
    Views:
    127
    Göktuğ Kayaalp
    May 31, 2013
  5. Skip Montanaro
    Replies:
    0
    Views:
    123
    Skip Montanaro
    May 31, 2013
Loading...

Share This Page