Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64374
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '(self,': 0.09; '__init__': 0.09; 'false.': 0.09; 'read-only': 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; 'argument.': 0.16; 'attempted': 0.16; 'b=2)': 0.16; 'charles': 0.16; 'created.': 0.16; 'defined.': 0.16; 'dict': 0.16; 'docstrings': 0.16; 'initializers': 0.16; 'key):': 0.16; 'mutable': 0.16; 'prefered': 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; 'subclass': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'instance,': 0.24; 'url:dev': 0.24; "haven't": 0.24; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; 'comments': 0.31; 'usually': 0.31; 'class': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'usual': 0.35; 'test': 0.35; 'found.': 0.36; 'method': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ensure': 0.60; 'new': 0.61; 'first': 0.61; 'default': 0.69; 'hoping': 0.75; 'dict.': 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 21:52:57 +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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5758.1390251160.18130.python-list@python.org> (permalink) |
| Lines | 58 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1390251160 news.xs4all.nl 2925 [2001:888:2000:d::a6]:33568 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:64374 |
Show key headers only | View raw
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}
> if not isinstance(ddict, dict):
> raise TypeError("ddict must be a dict. It is " +
> repr(ddict))
> self._ddict = ddict
I think instead of the type check I would build a new dict from the
argument. The usual initializers
dict({1:2, 3:4})
dict([(1,2), (3,4)])
dict(a=1, b=2)
should work with your read-only dict.
>
> ## Test for containment.
> # @param key The item to be found.
> # @return True If key is in the instance, otherwise False.
Docstrings are usually prefered over comments like the above.
> def __contains__(self, key):
> return key in self._ddict
Did you know
http://docs.python.org/dev/library/collections.abc.html#collections.abc.Mapping
Subclass from it to ensure that all the usuall methods are defined.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: 1st Sketch at at ReadOnly dict Peter Otten <__peter__@web.de> - 2014-01-20 21:52 +0100
csiph-web