Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'example:': 0.03; 'python.': 0.04; 'dictionary': 0.07; 'python': 0.08; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; 'copied.': 0.16; 'finney': 0.16; 'cc:addr:python-list': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'values': 0.25; 'function': 0.25; 'received:209.85.161': 0.26; 'object': 0.26; 'pass': 0.27; 'message-id:@mail.gmail.com': 0.28; 'reflect': 0.28; 'sat,': 0.29; 'import': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; 'changes': 0.30; 'looks': 0.31; 'this.': 0.31; 'andrew': 0.32; 'cases': 0.32; 'copying': 0.33; 'skip:" 10': 0.35; 'assignment': 0.35; 'copied': 0.35; 'ordinary': 0.35; 'actual': 0.36; 'probably': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'fair': 0.37; 'but': 0.38; 'data': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'received:209': 0.39; 'got': 0.39; 'world': 0.63; 'here': 0.66; 'subject:!': 0.67; '11,': 0.68; 'case;': 0.84; 'everything,': 0.84; 'want:': 0.84; '10:32': 0.91; 'absolutely': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type:content-transfer-encoding; bh=uLa+yKmX5qfic2SR+MLyjZAt2cdHxukWWV0mKS8J3l0=; b=YKf+yFcUFr7KkKwaIsijeMDdvXYY58PVIkvnwWRNqm6T0p209uKNeSA/bPKruXJ+AE Ed0ujp3DDTBMRnQh1Sz4UDoLw3K3FF0jRknfHXy7FZPESb5SXIsyRxJ8Py/Tq7iBW2CM Uv/RID9TfWl0XnDk95FEZahfgs8pfQVVaiytQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=NInm6+fiJGhrZ1VEzXJfOlhTEnr+s1SjZ0ws7xFF7YvKFlXz6UZiaB3RoUCF/Sweqo 80VVnejvUwUZEVYZ9QHuIYQsDBA5HSY3wzSZBXso1HKcKPX6fETBmdPU6Tg0l9QvewLC nji016Q/1IRsb2NJX9IU3C9jjDvGgZEky893Y= MIME-Version: 1.0 In-Reply-To: <4DF4414F.7000305@gmail.com> References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> <877h8rvhst.fsf@benfinney.id.au> <4DF4414F.7000305@gmail.com> From: Ian Kelly Date: Sat, 11 Jun 2011 23:12:54 -0600 Subject: Re: __dict__ is neato torpedo! To: Andrew Berg Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Cc: "comp.lang.python" 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: 64 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307855605 news.xs4all.nl 49182 [::ffff:82.94.164.166]:34954 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7474 On Sat, Jun 11, 2011 at 10:32 PM, Andrew Berg w= rote: > On 2011.06.11 10:40 PM, Ben Finney wrote: >> It's exactly the same as with an ordinary assignment (=91a =3D b=92) in >> Python. > Fair enough. >> > How would I make actual copies? >> At what level? > Level? I just want to be able to create an object b with values from > dictionary a, and not have changes to a reflect b and vice-versa once b > is created. It sounds like the copy.deepcopy function is what you want: >>> from copy import deepcopy >>> class X(object): pass ... >>> a =3D X() >>> a.fruit =3D ['apples'] >>> b =3D deepcopy(a) >>> a.fruit ['apples'] >>> b.fruit ['apples'] >>> a.fruit.append('oranges') >>> a.fruit ['apples', 'oranges'] >>> b.fruit ['apples'] >> =A0Or you can use the various >> methods in the =91copy=92 module depending on what you want. > copy.deepcopy() looks appealing, but I don't know what the docs mean by > "administrative data structures". It just means that you don't always want absolutely everything copied. For example: >>> class World(object): pass ... >>> class Actor(object): ... def __init__(self, world): ... self.world =3D world ... >>> class Action(object): ... def __init__(self, actor): ... self.actor =3D actor ... >>> a =3D Action(Actor(World())) >>> b =3D deepcopy(a) >>> a.actor is b.actor False >>> a.actor.world is b.actor.world False The intention here is probably that a and b should both be part of the same World, but as you can see that is not the case; the World got copied along with everything else. Python provides machinery to let you avoid deep copying absolutely everything, but it's important to be aware of cases like this. Cheers, Ian