surprised by import in python 2.6

Discussion in 'Python' started by Stefaan Himpe, Dec 10, 2010.

  1. Hello list,

    Recently someone asked me this question, to which I could not give an
    answer. I'm hoping for some insight, or a manual page. What follows is
    python 2.6.

    The problem is with the difference between

    from test import *

    and

    import test

    First things first. Here's the code to test.py:

    a = 3
    def f():
    global a
    return a

    Now, in the interactive interpreter:

    >>> from test import *
    >>> a

    3
    >>> f()

    3
    >>> a = 4
    >>> f()

    3

    in a second session:

    >>> import test
    >>> test.a

    3
    >>> test.f()

    3
    >>> test.a = 4
    >>> test.f()

    4

    Somehow, in the first session I cannot modify the global variable a
    returned from f, but in the second session I can. To my eye, the only
    difference seems to be a namespace. Can anyone shine some light on this
    matter?

    Thanks,
    Stefaan.
     
    Stefaan Himpe, Dec 10, 2010
    #1
    1. Advertising

  2. Stefaan Himpe

    Ian Guest

    On Dec 10, 3:06 pm, Stefaan Himpe <> wrote:
    > Somehow, in the first session I cannot modify the global variable a
    > returned from f, but in the second session I can. To my eye, the only
    > difference seems to be a namespace. Can anyone shine some light on this
    > matter?


    It's not the same global variable. In the second session, you import
    the module test and bind it to the name "test" in the main namespace.
    "test.a" and "test.f" refer to the objects named "a" and "f" in the
    test namespace.

    In the first session, you import all the variables exported by the
    module test and bind them using the same names in the main namespace.
    Thus "a" and "test.a" refer to the same int; and "f" and "test.f"
    refer to the same function, but they are not the same variables. When
    you rebind the name "a", it does not also magically rebind "test.a",
    and vice versa.

    Cheers,
    Ian
     
    Ian, Dec 10, 2010
    #2
    1. Advertising

  3. On 12/10/2010 2:22 PM Ian said...
    > On Dec 10, 3:06 pm, Stefaan Himpe<> wrote:
    >> Somehow, in the first session I cannot modify the global variable a
    >> returned from f, but in the second session I can. To my eye, the only
    >> difference seems to be a namespace. Can anyone shine some light on this
    >> matter?

    >
    > It's not the same global variable. In the second session, you import
    > the module test and bind it to the name "test" in the main namespace.
    > "test.a" and "test.f" refer to the objects named "a" and "f" in the
    > test namespace.
    >
    > In the first session, you import all the variables exported by the
    > module test and bind them using the same names in the main namespace.
    > Thus "a" and "test.a" refer to the same int; and "f" and "test.f"
    > refer to the same function, but they are not the same variables. When
    > you rebind the name "a", it does not also magically rebind "test.a",
    > and vice versa.
    >


    Here's an example of what Ian's explained showing that f's global
    namespace is the test module:


    emile@paj39:~$ cat > test.py
    a = 3
    def f():
    global a
    return a
    emile@paj39:~$ python
    Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11)
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from test import *
    >>> import test
    >>> a

    3
    >>> a = 4
    >>> f()

    3
    >>> test.a = 5
    >>> f()

    5
    >>>
     
    Emile van Sebille, Dec 10, 2010
    #3
  4. Stefaan Himpe

    Aahz Guest

    In article <TvxMo.3345$2>,
    Stefaan Himpe <> wrote:
    >
    >Recently someone asked me this question, to which I could not give an
    >answer. I'm hoping for some insight, or a manual page. What follows is
    >python 2.6.
    >
    >The problem is with the difference between
    >
    >from test import *
    >
    >and
    >
    >import test


    Just adding to this thread for Gooja:

    Don't use "import *" -- it makes debugging difficult because you can't
    tell where a name comes from.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Think of it as evolution in action." --Tony Rand
     
    Aahz, Jan 9, 2011
    #4
    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. John Harrison
    Replies:
    9
    Views:
    364
    John Harrison
    Feb 22, 2004
  2. Replies:
    0
    Views:
    292
  3. Replies:
    0
    Views:
    309
  4. Replies:
    0
    Views:
    284
  5. Replies:
    0
    Views:
    336
Loading...

Share This Page