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: 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> <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 To: Saqib Ali 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: 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 On Sat, Jul 2, 2011 at 3:23 PM, Saqib Ali 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): >> =C2=A0 =C2=A0 def __init__(self): >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.mySet =3D sets.Set(range(1,10)) >> >> =C2=A0 =C2=A0 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 =3D 7 foo_inst =3D Foo() then both `foo_inst.bar` and `Foo.bar` refer to the same value. However, if we then do: foo_inst.bar =3D 42 then we'll have: foo_inst.bar =3D=3D 42 and Foo.bar =3D=3D 7 Now back to your actual question. In clearNum(), you do: self.myNum =3D 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