Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'wed,': 0.04; 'int,': 0.07; 'foo': 0.09; 'namespace': 0.09; 'object.': 0.09; 'values,': 0.09; 'am,': 0.14; 'modify': 0.14; 'wrote:': 0.14; 'assigns': 0.16; 'cpython,': 0.16; 'did.': 0.16; 'immutable,': 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'set,': 0.16; 'subject:directly': 0.16; 'subject:those': 0.16; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'header:In-Reply- To:1': 0.22; 'cc:addr:python-list': 0.22; 'objects,': 0.23; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; '(in': 0.27; 'object': 0.27; 'message- id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'daniel': 0.29; 'cc:addr:python.org': 0.31; 'imported': 0.31; 'import': 0.32; 'module': 0.33; 'reference': 0.34; 'got': 0.34; 'point': 0.35; 'some': 0.37; 'received:209.85': 0.37; 'exactly': 0.37; 'references': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'current': 0.38; 'received:209.85.214': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'best': 0.60; '2011': 0.62; 'hand,': 0.72; 'subject:Why': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=AqSyytJ9QUDg7e6CCY2STV3WL1Zz5ro4dXbweXN4i5E=; b=FibaEKUNjgmRBWFtvKlx1cH9Fi/zDlZkdoKvaozoi5X8IigYke9d24OPN80eurepTy 9m/vBRAN4dZOq2WtDbfjLujmj/on45Jo5MyAmQ/gsUQYqSg4sXxNqXcjI3l704HcN069 SCIVyFKKzK1/PFc+syQ43N9w8EHrBonNZ4Gig= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=bY5udlA19T5mT2/5vaDaSc7WCpY3XLAVRPUMatzgnnoqbOZxtjux+QgP6pBIZDzNgm UFb8bepPa3ST9DKUvEW3L3G9rlMINDc6qjKdNWiliKaoN0z/OqBg7NNoSslHKI2/QbNZ oHHnduU1K7+JiwSkqgpxabreWM4EAZ6IR+Tzs= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 4 May 2011 03:57:51 +1100 Subject: Re: Why do directly imported variables behave differently than those attached to imported module? From: Daniel Kluev To: Dun Peal Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 22 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304441879 news.xs4all.nl 81475 [::ffff:82.94.164.166]:50675 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4555 On Wed, May 4, 2011 at 3:31 AM, Dun Peal 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