Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8686
| 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!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.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'python.': 0.04; '*not*': 0.05; 'instance': 0.05; 'method,': 0.07; 'variable,': 0.07; 'rewritten': 0.09; 'wrote:': 0.15; 'instantiate': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'subject:behavior': 0.16; 'subject:example': 0.16; 'subject:set': 0.16; 'class,': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'def': 0.16; 'programming': 0.18; 'cheers,': 0.19; "haven't": 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; 'do,': 0.28; 'changing': 0.28; 'sat,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'variables': 0.29; 'cc:addr:python.org': 0.30; 'skip:\xc2 20': 0.30; 'class': 0.31; 'shared': 0.32; 'chris': 0.32; 'me?': 0.33; 'someone': 0.34; '(as': 0.34; "can't": 0.34; 'thus,': 0.35; 'pretty': 0.35; 'explain': 0.36; 'url:python': 0.37; 'affects': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'created': 0.38; 'correctly': 0.39; 'url:docs': 0.39; 'skip:s 20': 0.39; 'received:209': 0.40; 'official': 0.40; 'your': 0.60; 'life': 0.64; 'strange': 0.68; 'as:': 0.71; 'before...': 0.84; 'good:': 0.84; 'saqib': 0.84; 'sender:addr:chris': 0.84; 'subject:class': 0.84; 'url:rebertia': 0.84; 'touched': 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=bqG+FrQMOTx2C67otFVjouY1uoaf6fu+Nopea1hDT9Q=; b=EkEkiXDoYxzWb51c6TwUmwO1tPFv9FVhj+QTEbQYvA5hriyTujVDdUwrtplfcMCnUY fAiLwH38hKf3sCs/exVJTsasaA0AVOyjkgOZ7c3ylwj/U7fnNqyBrOFjDCLVW3CNRNxQ VBFj1Q2zcGHKhcHdZArFgYPz0YirkwtBtdm+0= |
| MIME-Version | 1.0 |
| Sender | chris@rebertia.com |
| In-Reply-To | <2dc52e22-5c0f-4e24-8197-df616db1a42c@u2g2000yqb.googlegroups.com> |
| References | <2dc52e22-5c0f-4e24-8197-df616db1a42c@u2g2000yqb.googlegroups.com> |
| Date | Sat, 2 Jul 2011 15:14:02 -0700 |
| X-Google-Sender-Auth | _K5Gcd_b3ZvfYaVQC-hxYi3uTFE |
| 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.566.1309644846.1164.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1309644846 news.xs4all.nl 21885 [2001:888:2000:d::a6]:43751 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:8686 |
Show key headers only | View raw
On Sat, Jul 2, 2011 at 2:59 PM, Saqib Ali <saqib.ali.75@gmail.com> wrote:
<snip>
> Then I instantiate 2 instances of myClass2 (c & d). I then change the
> value of c.mySet. Bizarrely changing the value of c.mySet also affects
> the value of d.mySet which I haven't touched at all!?!?! Can someone
> explain this very strange behavior to me? I can't understand it for
> the life of me.
<snip>
> class myClass2:
>
> mySet = sets.Set(range(1,10))
>
> def clearSet(self):
> self.mySet.clear()
>
> def __str__(self):
> return str(len(self.mySet))
Please read a tutorial on object-oriented programming in Python. The
official one is pretty good:
http://docs.python.org/tutorial/classes.html
If you do, you'll find out that your class (as written) has mySet as a
class (Java lingo: static) variable, *not* an instance variable; thus,
it is shared by all instances of the class, and hence the behavior you
observed. 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...
Cheers,
Chris
--
http://rebertia.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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