Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(self,': 0.09; '__init__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '42,': 0.16; 'attempted': 0.16; 'charles': 0.16; 'created.': 0.16; 'dict': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'header:User-Agent:1': 0.23; "haven't": 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; "i'm": 0.30; '>>>>': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'test': 0.35; 'method': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'matter': 0.61; 'course': 0.61; 'first': 0.61; 'default': 0.69; 'hoping': 0.75; 'otten': 0.84; 'approach.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: 1st Sketch at at ReadOnly dict Date: Mon, 20 Jan 2014 22:00:10 +0100 Organization: None References: <52DD8297.9050504@earthlink.net> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bf3a.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390251593 news.xs4all.nl 2883 [2001:888:2000:d::a6]:36313 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64375 Peter Otten wrote: > Charles Hixson wrote: > >> This is just a first sketch, and I haven't yet attempted to test it, so >> what I'm hoping for is criticisms on the general approach. >> >> class RODict: > >> def __init__ (self, ddict = {}): > > Default values are evaluted just once when the method is created. Mutable > default values mean trouble: > >>>> class D: > ... def __init__(self, dict={}): > ... self.dict = dict > ... def __setitem__(self, key, value): > ... self.dict[key] = value > ... def __repr__(self): return repr(self.dict) > ... >>>> d1 = D() >>>> d2 = D() >>>> d1[1] = 42 >>>> d2[2] = 42 >>>> d1 > {1: 42, 2: 42} >>>> d2 > {1: 42, 2: 42} D'oh, that was just and instintive reaction. You may already know that... Of course it doesn't matter as long as no attempt is made to mutate the mutable value.