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!newsfeed.xs4all.nl!newsfeed6.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'constructor': 0.07; 'dictionary': 0.07; 'interpreter': 0.07; 'method,': 0.07; 'used.': 0.07; 'python': 0.08; 'dict': 0.09; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'received:209.85.214.174': 0.14; 'received:mail-iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'aware,': 0.16; 'finney': 0.16; 'immutable': 0.16; 'input': 0.17; 'meant': 0.18; 'header:In-Reply-To:1': 0.21; 'fine': 0.22; 'restrict': 0.23; 'code': 0.24; 'values': 0.25; 'point,': 0.25; 'object': 0.26; 'tried': 0.27; 'reflect': 0.28; 'skip:_ 20': 0.28; 'received:209.85.214': 0.28; 'explicitly': 0.29; 'class': 0.29; 'module': 0.30; 'changes': 0.30; 'looks': 0.31; 'to:addr:python- list': 0.33; 'list': 0.33; 'actually': 0.33; 'header:User- Agent:1': 0.35; 'skip:" 10': 0.35; 'assignment': 0.35; 'ordinary': 0.35; 'message-id:@gmail.com': 0.36; 'actual': 0.36; 'probably': 0.36; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'affects': 0.37; 'fair': 0.37; 'think': 0.38; 'could': 0.38; 'problem.': 0.38; 'but': 0.38; 'data': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'received:192': 0.38; 'skip:s 20': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'received:192.168.1': 0.40; 'your': 0.60; 'ever': 0.64; 'subject:!': 0.67; 'making': 0.67; 'feeding': 0.67; 'here.': 0.69 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=fNY3Jz3i7PXKijVvKp5+X5a+/OGieLj3IKKMFkg3oYw=; b=deYyImhJIJmSCpPmoKrKKICPOtsV+Zj008VLthqJl7ycBuo+bEAZRni0/qdC92/Isf qfEoDySiWZzCNf9RPe05jLAA0qhyPl7pU5Xr3X0jRG2UELSH8WHJfuJr67NWVPxYQehc yJ8dQuLgzvSkekYOasuDn0Tn/pQn9EKUk7HIg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; b=uoGdih8r3ijPVsxTmfaGJAgS676h/z/X2uad4vaCIvxYcmJSa0WMuTzc5BhzKPyN0G uOjvA+CnzZgeZxkmxQPM413ccLyhIEdE1UaPe2p6hGGBNv3fMI1plduCD9mnqqb8MP3T jerWzRpmCVa9tocnAOgI76FHj6yX0l/getJwU= Date: Sat, 11 Jun 2011 23:32:15 -0500 From: Andrew Berg User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5 MIME-Version: 1.0 To: "comp.lang.python" Subject: Re: __dict__ is neato torpedo! References: <4DF41735.60307@gmail.com> <4DF422A8.303@gmail.com> <877h8rvhst.fsf@benfinney.id.au> In-Reply-To: <877h8rvhst.fsf@benfinney.id.au> X-Enigmail-Version: 1.1.1 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: 36 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307853144 news.xs4all.nl 49176 [::ffff:82.94.164.166]:57397 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7472 On 2011.06.11 10:40 PM, Ben Finney wrote: > It's exactly the same as with an ordinary assignment (‘a = b’) 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. > You can create a new dict or a new list by feeding the > esiting one to the constructor for the type. Not sure what you mean here. I thought you meant it copies a dictionary when used as input in an __init__() method, but I tried that in the interpreter and a still affects b: >>> class testObj(): ... def __init__(self,input): ... self.__dict__.update(input) ... >>> a = dict(list=['one', 'two'], str='hello') >>> b = testObj(a) >>> a['list'].append('three') >>> a {'list': ['one', 'two', 'three'], 'str': 'hello'} >>> b.list ['one', 'two', 'three'] > Or you can use the various > methods in the ‘copy’ module depending on what you want. copy.deepcopy() looks appealing, but I don't know what the docs mean by "administrative data structures". > Be aware, though, that most Python code gets by just fine without > explicitly making copies, and I hardly ever see the ‘copy’ module > actually used. Now that I think about it, I could probably restrict myself to immutable types inside the dictionary without much problem. Was that your point, or did you mean something else?