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


Groups > comp.lang.python > #8692

Re: Inexplicable behavior in simple example of a set in a class

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '*not*': 0.05; 'instance': 0.05; 'method,': 0.07; 'skip:` 10': 0.07; 'created,': 0.09; 'integer,': 0.09; 'rewritten': 0.09; 'wrote:': 0.15; 'curiosity,': 0.16; 'instances.': 0.16; 'lookup': 0.16; 'subject:behavior': 0.16; 'subject:example': 0.16; 'subject:set': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'question.': 0.16; 'def': 0.16; 'example.': 0.19; 'have:': 0.19; 'cheers,': 0.19; 'variable': 0.21; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'body.': 0.23; 'thus': 0.23; 'sat,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'variables': 0.29; 'object': 0.30; 'cc:addr:python.org': 0.30; 'looks': 0.30; 'thanks': 0.31; 'class': 0.31; 'shared': 0.32; 'chris': 0.32; 'certainly': 0.32; 'value.': 0.32; 'does': 0.32; 'calling': 0.34; 'however,': 0.34; 'creates': 0.34; 'that,': 0.35; 'actual': 0.35; 'explain': 0.36; 'but': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'created': 0.38; 'correctly': 0.39; 'skip:s 20': 0.39; 'received:74.125': 0.40; 'your': 0.60; 'back': 0.63; 'further': 0.65; 'as:': 0.71; 'url:htm': 0.72; 'before...': 0.84; 'contrast,': 0.84; 'saqib': 0.84; 'sender:addr:chris': 0.84; 'subject:class': 0.84; 'url:rebertia': 0.84; 'order:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; 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; bh=byfsTT5qPeiU26yHGgCFUOxyS1N81RFswLp+GJ4r8VI=; b=QCqxM9wHEDBB0WfX/j8bD9kRr4/t2HQFOQfaoB3rZSgjb94MwMh3ewGuQ/pY5j/FV8 JUucdKpHqQ6/yA4ONUy8rp+KeMkbcBgWY3JU9vYUptdyO4vD/9fWCS/IgTE2B17hR2aa AmQWagobtvMzZ6wm0avPxJgtM3tTc6lVvW5/o=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <6441039f-f226-4632-8e74-2b6415bf914e@n28g2000vbs.googlegroups.com>
References <2dc52e22-5c0f-4e24-8197-df616db1a42c@u2g2000yqb.googlegroups.com> <mailman.566.1309644846.1164.python-list@python.org> <6441039f-f226-4632-8e74-2b6415bf914e@n28g2000vbs.googlegroups.com>
Date Sat, 2 Jul 2011 16:25:45 -0700
X-Google-Sender-Auth RnmFAIdSq1MMcVW1oDNV2hqnS8E
Subject Re: Inexplicable behavior in simple example of a set in a class
From Chris Rebert <clp2@rebertia.com>
To Saqib Ali <saqib.ali.75@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.567.1309649149.1164.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1309649149 news.xs4all.nl 21748 [2001:888:2000:d::a6]:59030
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8692

Show key headers only | View raw


On Sat, Jul 2, 2011 at 3:23 PM, Saqib Ali <saqib.ali.75@gmail.com> wrote:
>> Instance variables are properly created in the __init__()
>> initializer method, *not* directly in the class body.
>>
>> Your class would be correctly rewritten as:
>>
>> class MyClass2(object):
>>     def __init__(self):
>>         self.mySet = sets.Set(range(1,10))
>>
>>     def clearSet(self):
>> # ...rest same as before...
>
>
> Thanks Chris. That was certainly very helpful!!
>
> So just out of curiosity, why does it work as I had expected when the
> member contains an integer, but not when the member contains a set?

To explain that, one must first understand that name lookup on an
object looks in the following places, in order:
1. the instance itself
2. the instance's class
3. the instance's superclasses

So, if we have:

class Foo(object):
    bar = 7
foo_inst = Foo()

then both `foo_inst.bar` and `Foo.bar` refer to the same value.

However, if we then do:

foo_inst.bar = 42

then we'll have:

foo_inst.bar == 42 and Foo.bar == 7


Now back to your actual question. In clearNum(), you do:
    self.myNum = 0
which creates a *new* instance variable that shadows the class
variable of the same name, like in my example. If you check, you'll
indeed see that myClass1.myNum is still 9 after calling clearNum().

By contrast, in clearSet() you do:
    self.mySet.clear()
which just mutates the existing Set object in-place. No new variable
is created, and mySet is still a class variable and thus shared by all
instances.

Further reading:
http://effbot.org/zone/python-objects.htm
http://effbot.org/zone/call-by-object.htm

Cheers,
Chris
--
http://rebertia.com

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


Thread

Inexplicable behavior in simple example of a set in a class Saqib Ali <saqib.ali.75@gmail.com> - 2011-07-02 14:59 -0700
  Re: Inexplicable behavior in simple example of a set in a class Chris Rebert <clp2@rebertia.com> - 2011-07-02 15:14 -0700
    Re: Inexplicable behavior in simple example of a set in a class Saqib Ali <saqib.ali.75@gmail.com> - 2011-07-02 15:23 -0700
      Re: Inexplicable behavior in simple example of a set in a class Chris Rebert <clp2@rebertia.com> - 2011-07-02 16:25 -0700
      Re: Inexplicable behavior in simple example of a set in a class Chris Angelico <rosuav@gmail.com> - 2011-07-03 10:46 +1000
      Re: Inexplicable behavior in simple example of a set in a class Chris Rebert <clp2@rebertia.com> - 2011-07-02 18:07 -0700
      Re: Inexplicable behavior in simple example of a set in a class Chris Angelico <rosuav@gmail.com> - 2011-07-03 11:14 +1000
  Re: Inexplicable behavior in simple example of a set in a class Peter Otten <__peter__@web.de> - 2011-07-03 00:22 +0200
  Re: Inexplicable behavior in simple example of a set in a class Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-03 12:25 +1000

csiph-web