Article View: pl.comp.lang.python
Article #27507Re: Magiczne dzieci
From: Artur M. Piwko
Date: Fri, 11 Jun 2010 05:55
Date: Fri, 11 Jun 2010 05:55
29 lines
1771 bytes
1771 bytes
In the darkest hour on Thu, 10 Jun 2010 23:09:26 +0200,
Wojciech Muła <wojciech_mula@poczta.null.onet.pl.invalid> screamed:
>> class A(object):
>> def __init__(self, name=None, children={}):
>> self.name = name
> print id(children)
>> self.children = children
> print id(self.children)
>
> 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)
>
Albo:
def __init__(self, name=None, children=None):
self.children = {} if children is None else children
--
[ Artur M. Piwko : Pipen : AMP29-RIPE : RLU:100918 : From == Trap! : SIG:234B ]
[ 07:54:37 user up 12465 days, 19:49, 1 user, load average: 0.36, 0.95, 0.00 ]
If you've got them by the balls their hearts and minds will follow.
Technical Details
Message-ID:
<slrni13jvd.er2.milusi.pysiaczek@buziaczek.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mailReferences:
<slrni12ker.eq7.beorn@pino.cyberdeck.pl> <20100610230926.668d6bf0.wojciech_mula@poczta.null.onet.pl.invalid>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail From: "Artur M. Piwko" <milusi.pysiaczek@buziaczek.pl> Newsgroups: pl.comp.lang.python Subject: Re: Magiczne dzieci Date: Fri, 11 Jun 2010 07:55:57 +0200 Organization: Petroleum Drinkers (; Lines: 29 Message-ID: <slrni13jvd.er2.milusi.pysiaczek@buziaczek.pl> References: <slrni12ker.eq7.beorn@pino.cyberdeck.pl> <20100610230926.668d6bf0.wojciech_mula@poczta.null.onet.pl.invalid> Reply-To: milusi.pysiaczek@buziaczek.pl NNTP-Posting-Host: 81.26.8.20 Mime-Version: 1.0 X-Trace: inews.gazeta.pl 1276236481 5593 81.26.8.20 (11 Jun 2010 06:08:01 GMT) X-Complaints-To: usenet@agora.pl NNTP-Posting-Date: Fri, 11 Jun 2010 06:08:01 +0000 (UTC) X-User: pipene User-Agent: slrn/pre1.0.0-18 (Linux) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit