Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64375 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2014-01-20 22:00 +0100 |
| Last post | 2014-01-20 22:00 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: 1st Sketch at at ReadOnly dict Peter Otten <__peter__@web.de> - 2014-01-20 22:00 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-01-20 22:00 +0100 |
| Subject | Re: 1st Sketch at at ReadOnly dict |
| Message-ID | <mailman.5759.1390251593.18130.python-list@python.org> |
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.
Back to top | Article view | comp.lang.python
csiph-web