Article View: pl.comp.lang.python
Article #27505Re: Magiczne dzieci
From: Wojciech Muła
Date: Thu, 10 Jun 2010 21:09
Date: Thu, 10 Jun 2010 21:09
23 lines
1401 bytes
1401 bytes
Daniel Mróz <beorn@alpha.pl> wrote:
> class A(object):
> def __init__(self, name=None, children={}):
> self.name = name
print id(children)
> self.children = children
print id(self.children)
>
>
> def add_child(self, child):
> self.children[child.name] = child
Jak uruchomisz, zobaczysz, że wszystkie instancje klasy dostają
ten sam obiekt słownika i na nim operują. Spróbuj dać na początku
import copy
a w konstruktorze
self.children = copy.copy(children)
w.
Technical Details
Message-ID:
<20100610230926.668d6bf0.wojciech_mula@poczta.null.onet.pl.invalid>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!not-for-mailReferences:
<slrni12ker.eq7.beorn@pino.cyberdeck.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!not-for-mail From: Wojciech Muła <wojciech_mula@poczta.null.onet.pl.invalid> Newsgroups: pl.comp.lang.python Subject: Re: Magiczne dzieci Date: Thu, 10 Jun 2010 23:09:26 +0200 Organization: http://onet.pl Lines: 23 Message-ID: <20100610230926.668d6bf0.wojciech_mula@poczta.null.onet.pl.invalid> References: <slrni12ker.eq7.beorn@pino.cyberdeck.pl> NNTP-Posting-Host: public-gprs76035.centertel.pl Mime-Version: 1.0 X-Trace: news.onet.pl 1276204177 4283 91.94.170.89 (10 Jun 2010 21:09:37 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Thu, 10 Jun 2010 21:09:37 +0000 (UTC) X-Newsreader: Sylpheed 2.5.0 (GTK+ 2.10.14; i686-pc-mingw32) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit