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


Groups > comp.lang.python > #4582

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

References <d8d1d20e-2edd-49e5-917c-f338ef35006f@l18g2000yql.googlegroups.com> <BANLkTikT3qeeMAToUhO7GDrve5fAJ_+vFg@mail.gmail.com> <BANLkTik1bNumywuwMiQHPS6kTsf7ig+3Pw@mail.gmail.com>
Date 2011-05-03 15:11 -0700
Subject Re: Why do directly imported variables behave differently than those attached to imported module?
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.1129.1304460710.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, May 3, 2011 at 2:47 PM, Chris Angelico <rosuav@gmail.com> wrote:
> On Wed, May 4, 2011 at 2:57 AM, Chris Rebert <clp2@rebertia.com> wrote:
>> from foo import *
>>
>> can be thought of as essentially doing:
>>
>> import foo
>> set = foo.set
>> var = foo.var
>> del foo
>
> Here's a side point. What types will hold a reference to the enclosing
> module (or at least its dictionary)? Would it be possible to use a
> from import to load a module, then "lose" the module even though
> you're using objects from it?
>
> I am guessing that a function or class will hold such a reference,
> because otherwise it would be a bit awkward for them to use any sort
> of module-level state. Or do they not, and instead go look for their
> module in sys.modules?

some_python_function.__globals__ is the exact reference you postulated.
Classes don't seem to hold such references; their methods do though
(by virtue of being function objects), which makes sense.

Cheers,
Chris
--
http://rebertia.com

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


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