Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:module': 0.04; 'int': 0.05; 'suppose': 0.05; 'terry': 0.07; 'threads,': 0.07; 'python': 0.07; 'bind': 0.09; 'builtin': 0.09; 'dict': 0.09; 'foo': 0.09; 'obsolete': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'pm,': 0.11; 'written': 0.12; 'def': 0.13; 'wrote:': 0.14; "'as'": 0.16; 'module:': 0.16; 'reedy': 0.16; 'subject:directly': 0.16; 'subject:those': 0.16; 'class,': 0.16; 'entries': 0.16; 'jan': 0.22; 'header:In-Reply- To:1': 0.22; 'script': 0.26; 'instead': 0.26; 'problem': 0.29; 'subject:?': 0.29; 'discussed': 0.29; 'class': 0.29; 'changes': 0.29; 'bound': 0.29; 'one,': 0.31; 'augmented': 0.31; 'binding': 0.31; 'books.': 0.31; 'objects.': 0.31; 'reveal': 0.31; 'does': 0.31; 'entry': 0.32; 'perhaps': 0.32; 'import': 0.32; 'to:addr :python-list': 0.32; 'idea': 0.32; 'creates': 0.33; 'module': 0.33; 'using': 0.34; 'header:X-Complaints-To:1': 0.34; 'change': 0.34; 'print': 0.35; 'header:User-Agent:1': 0.35; 'function.': 0.35; 'surprised': 0.35; 'two': 0.37; 'it?': 0.37; 'current': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'could': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'book.': 0.60; 'choice.': 0.60; 'best': 0.60; 'address': 0.61; 'subject': 0.61; 'var': 0.65; 'discovered': 0.71; "'set'": 0.84; 'dict,': 0.84; 'subject:Why': 0.84; 'var,': 0.84; 'yours.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Why do directly imported variables behave differently than those attached to imported module? Date: Tue, 03 May 2011 13:13:12 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304442808 news.xs4all.nl 81478 [::ffff:82.94.164.166]:52021 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4559 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