Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4555

Re: Why do directly imported variables behave differently than those attached to imported module?

References <d8d1d20e-2edd-49e5-917c-f338ef35006f@l18g2000yql.googlegroups.com>
Date 2011-05-04 03:57 +1100
Subject Re: Why do directly imported variables behave differently than those attached to imported module?
From Daniel Kluev <dan.kluev@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1115.1304441878.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, May 4, 2011 at 3:31 AM, Dun Peal <dunpealer@gmail.com> wrote:
> Apparently, the `var` we imported from `foo` never got set, but
> `foo.var` on the imported `foo` - did. Why?

Because all names are references to some values, not other names (in
CPython, it means all names are PyObject*, and point directly to the
objects, not other pointers)

When you do `from foo import bar` it assigns globals()['bar'] of
current module to reference same value as `foo.bar`. Its now local
namespace name, not `foo` namespace, and therefore functions in `foo`
cannot modify this namespace.

Since ints are immutable, when you do `var = 1` you create new object
of type int, and re-assign `var` name to point to new object.

`foo.var`, on other hand, is a way to access `foo`'s own namespace, so
its exactly same name as globals()['var'] of `foo`.

-- 
With best regards,
Daniel Kluev

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why do directly imported variables behave differently than those attached to imported module? Dun Peal <dunpealer@gmail.com> - 2011-05-03 09:31 -0700
  Re: Why do directly imported variables behave differently than those attached to imported module? Chris Rebert <clp2@rebertia.com> - 2011-05-03 09:57 -0700
  Re: Why do directly imported variables behave differently than those attached to imported module? Daniel Kluev <dan.kluev@gmail.com> - 2011-05-04 03:57 +1100
  Re: Why do directly imported variables behave differently than those attached to imported module? Mel <mwilson@the-wire.com> - 2011-05-03 13:00 -0400
  Re: Why do directly imported variables behave differently than those attached to imported module? Terry Reedy <tjreedy@udel.edu> - 2011-05-03 13:13 -0400
  Re: Why do directly imported variables behave differently than those attached to imported module? harrismh777 <harrismh777@charter.net> - 2011-05-03 12:24 -0500
    Re: Why do directly imported variables behave differently than those attached to imported module? Dun Peal <dunpealer@gmail.com> - 2011-05-03 11:51 -0700
      Re: Why do directly imported variables behave differently than those attached to imported module? Dun Peal <dunpealer@gmail.com> - 2011-05-03 12:23 -0700
        Re: Why do directly imported variables behave differently than those attached to imported module? Daniel Kluev <dan.kluev@gmail.com> - 2011-05-04 06:55 +1100
  Re: Why do directly imported variables behave differently than those attached to imported module? Chris Angelico <rosuav@gmail.com> - 2011-05-04 07:47 +1000
    Re: Why do directly imported variables behave differently than those attached to imported module? Duncan Booth <duncan.booth@invalid.invalid> - 2011-05-04 08:44 +0000
  Re: Why do directly imported variables behave differently than those attached to imported module? Chris Rebert <clp2@rebertia.com> - 2011-05-03 15:11 -0700

csiph-web