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; 'anyway': 0.03; 'subject:module': 0.04; 'new,': 0.05; 'imports': 0.07; 'names.': 0.07; 'alias': 0.09; 'arguments.': 0.09; 'bind': 0.09; 'foo': 0.09; 'integers': 0.09; 'values,': 0.09; '>>>': 0.12; 'essentially': 0.12; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; '9:31': 0.16; 'did.': 0.16; 'doing:': 0.16; 'module:': 0.16; 'mutated': 0.16; 'obj,': 0.16; 'set,': 0.16; 'subject:directly': 0.16; 'subject:those': 0.16; '(i.e.': 0.18; 'tue,': 0.20; 'cc:no real name:2**0': 0.20; 'cc:2**0': 0.20; 'cheers,': 0.20; '(which': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; '(and': 0.22; 'values': 0.23; 'integer': 0.23; 'example': 0.24; 'received:209.85.220': 0.26; 'script': 0.26; 'chris': 0.27; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'effect': 0.29; 'cc:addr:python.org': 0.31; 'imported': 0.31; 'name;': 0.31; 'separate': 0.31; 'import': 0.32; 'module': 0.33; 'using': 0.34; 'got': 0.34; 'rather': 0.36; 'received:209.85': 0.37; 'references': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'code:': 0.38; 'current': 0.38; 'set': 0.39; 'received:209': 0.39; 'behavior': 0.40; 'would': 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'var': 0.65; 'binding.': 0.84; 'subject:Why': 0.84; 'var,': 0.84; 'comparable': 0.91; 'absolutely': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=Soc8UG1sbGqn/QfF4quJ56tcBphXBJ8bpeWQ8dck/K4=; b=GyHpoce7h4sdM1TpnB1T01euMp7E2zrIgNQwgTUxsWol5/8q4GnKDyBxtn5j4y/5u+ rFbJ2xsqMmonMYQQ/H+9JN2f+WMgtnVIIpEFVBMW26cJJ8KJYjw1f+LkiKaDXu7CxOqB /ifXv/+FSkE44e92E85Mv2E+s1Y2hZmzo9abA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=KSU9JMI2SyEKkwP4fTXoIGfNxZYqqS08HeFNzN0+NC7S3fXcru/1oAoSR5XoOhQCJU zd0kdIDbmKqD0dXR9rk0LD7isGaS/3IrGtreo/tP8HViTwGSXFtm1WsAE6rtVln3t6OK sZlzA7LRkgqj29WBZDgzNcF0sZNtUFp6cEkC0= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Tue, 3 May 2011 09:57:53 -0700 X-Google-Sender-Auth: uwBjI7mlIpJCVFTgtCH4xYaDPi8 Subject: Re: Why do directly imported variables behave differently than those attached to imported module? From: Chris Rebert To: Dun Peal Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 70 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1304441879 news.xs4all.nl 41113 [::ffff:82.94.164.166]:50674 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4554 On Tue, May 3, 2011 at 9:31 AM, Dun Peal wrote: > Hi! > > Here's the demonstrating code: > > =C2=A0 =C2=A0# module foo.py > =C2=A0 =C2=A0var =3D 0 > > =C2=A0 =C2=A0def set(): > =C2=A0 =C2=A0 =C2=A0 =C2=A0global var > =C2=A0 =C2=A0 =C2=A0 =C2=A0var =3D 1 > > Script using this module: > > =C2=A0 =C2=A0import foo > =C2=A0 =C2=A0from foo import * > > =C2=A0 =C2=A0print var, foo.var > =C2=A0 =C2=A0set() > =C2=A0 =C2=A0print var, foo.var > > Script output: > > =C2=A0 =C2=A00 0 > =C2=A0 =C2=A00 1 > > Apparently, the `var` we imported from `foo` never got set, but > `foo.var` on the imported `foo` - did. Why? Because imports (and assignments generally) bind names to values, they don't alias names to other names. from foo import * can be thought of as essentially doing: import foo set =3D foo.set var =3D foo.var del foo So the new, separate name __main__.var gets the current value of foo.var at import-time, which is the integer 0. You then call foo.set(), which re-binds foo.var to a new value (i.e. 1) rather than mutating the existing value (which would be impossible anyway since integers are immutable). This has absolutely no effect on __main__.var, which is an entirely separate binding. The behavior is comparable to that of function arguments. Values can be mutated, but re-binding names has only local effect: >>> a =3D 0 >>> def incr(b): ... b =3D 1 # rebinds local name b ... >>> incr(a) >>> a # outside name unaffected, just like in your example 0 >>> c =3D [7] >>> def mutate_then_rebind(b): ... b.append(99) # mutates passed-in value ... b =3D [42] # rebinds local name; has no outside effect ... >>> mutate_then_rebind(c) >>> c # name still references same obj, but that obj has been mutated [7, 99] Cheers, Chris -- http://rebertia.com