Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #4559
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Why do directly imported variables behave differently than those attached to imported module? |
| Date | 2011-05-03 13:13 -0400 |
| References | <d8d1d20e-2edd-49e5-917c-f338ef35006f@l18g2000yql.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1117.1304442807.9059.python-list@python.org> (permalink) |
Your problem is reveal in the subject line. As discussed in many other threads, including a current one, Python does not have 'variables' in the way that many understand the term. Python binds names to objects. Binding statements (assignment, augmented assignment, import, def, class, and others with 'as' clauses) all bind names to objects. Suppose we both have address books. You copy all entries from my book to yours. Afterwards, I update an entry in my book. That does not change the now obsolete entry in your book. On 5/3/2011 12:31 PM, Dun Peal wrote: > # module foo.py > var = 0 > > def set(): > global var > var = 1 Module foo's dict now has two entries for 'var' and 'set' bound to int 0 and a function. Note that 'set' is a builtin class name so not the best choice. > > Script using this module: Script creates module '__main__' > import foo This binds 'foo' to module foo in __main_'s dict. > from foo import * This copies foo's dict to __main__'s dict. > print var, foo.var > set() This changes foo's dict, rebinding 'var' to int 1. It does not change __main__'s dict. How could it? In the same way, 'var = 2' would leave foo's dict unchanged. __main__.var. > print var, foo.var > > Script output: > > 0 0 > 0 1 If instead of 'from foo import *', you had written 'from foo import var as rav', then perhaps you would not have been so surprised that __main__.rav and foo.var have independent fates. You have discovered that using from x import * is a bad idea when module x has global names that get rebound. -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
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