Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4556
| From | Mel <mwilson@the-wire.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Why do directly imported variables behave differently than those attached to imported module? |
| Followup-To | comp.lang.python |
| Date | 2011-05-03 13:00 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <ippcba$qt0$1@speranza.aioe.org> (permalink) |
| References | <d8d1d20e-2edd-49e5-917c-f338ef35006f@l18g2000yql.googlegroups.com> |
Followups directed to: comp.lang.python
Dun Peal wrote: > Hi! > > Here's the demonstrating code: > > # module foo.py > var = 0 > > def set(): > global var > var = 1 > > Script using this module: > > import foo > from foo import * > > print var, foo.var > set() > print var, foo.var > > Script output: > > 0 0 > 0 1 > > Apparently, the `var` we imported from `foo` never got set, but > `foo.var` on the imported `foo` - did. Why? They're different because -- they're different. `foo.var` is defined in the namespace of the foo module. Introspectively, you would access it as `foo.__dict__['var']` . Plain `var` is in your script's namespace so you could access it as `globals()['var']` . The values given to the vars are immutable integers, so assignment works by rebinding. The two different bindings in foo.__dict__ and globals() get bound to different integer objects. Note too the possible use of `globals()['foo'].__dict__['var'] . (Hope there are no typos in this post.) Mel.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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