Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4554

Re: Why do directly imported variables behave differently than those attached to imported module?

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 <chris@rebertia.com>
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 <d8d1d20e-2edd-49e5-917c-f338ef35006f@l18g2000yql.googlegroups.com>
References <d8d1d20e-2edd-49e5-917c-f338ef35006f@l18g2000yql.googlegroups.com>
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 <clp2@rebertia.com>
To Dun Peal <dunpealer@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1114.1304441878.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Tue, May 3, 2011 at 9:31 AM, Dun Peal <dunpealer@gmail.com> 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?

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 = foo.set
var = 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 = 0
>>> def incr(b):
...     b = 1 # rebinds local name b
...
>>> incr(a)
>>> a # outside name unaffected, just like in your example
0
>>> c = [7]
>>> def mutate_then_rebind(b):
...     b.append(99) # mutates passed-in value
...     b = [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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


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