Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64380
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <charleshixsn@earthlink.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; '(self,': 0.09; '__init__': 0.09; 'false.': 0.09; 'implemented.': 0.09; 'read-only': 0.09; 'def': 0.12; 'anyway': 0.14; '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; 'link,': 0.16; 'message- id:@earthlink.net': 0.16; 'mutable': 0.16; 'otoh,': 0.16; 'prefered': 0.16; 'received:dsl.mindspring.com': 0.16; 'separated': 0.16; 'subclass': 0.16; 'wrote:': 0.18; 'examples': 0.20; 'header:User-Agent:1': 0.23; 'instance,': 0.24; 'specify': 0.24; 'url:dev': 0.24; "haven't": 0.24; 'looks': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'raise': 0.29; "doesn't": 0.30; "i'm": 0.30; 'comments': 0.31; 'usually': 0.31; '(unless': 0.31; '>>>>': 0.31; 'apparently': 0.31; 'though.': 0.31; 'class': 0.32; 'handled': 0.32; 'url:python': 0.33; 'implemented': 0.33; 'skip:_ 10': 0.34; 'received:66': 0.35; "can't": 0.35; 'usual': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; '(e.g.,': 0.36; 'found.': 0.36; 'in.': 0.36; 'method': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'being': 0.38; 'mapping': 0.38; 'url:library': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'ensure': 0.60; 'tell': 0.60; 'new': 0.61; 'first': 0.61; 'show': 0.63; 'realized': 0.68; 'default': 0.69; 'hoping': 0.75; 'around,': 0.84; 'dict.': 0.84; 'otten': 0.84; 'approach.': 0.91 |
| Date | Mon, 20 Jan 2014 15:37:14 -0800 |
| From | Charles Hixson <charleshixsn@earthlink.net> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: 1st Sketch at at ReadOnly dict |
| References | <52DD8297.9050504@earthlink.net> <lbk29u$oho$1@ger.gmane.org> |
| In-Reply-To | <lbk29u$oho$1@ger.gmane.org> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| 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.5763.1390261121.18130.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1390261121 news.xs4all.nl 2957 [2001:888:2000:d::a6]:33894 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:64380 |
Show key headers only | View raw
On 01/20/2014 12:52 PM, 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}
>
>> 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.
>
That link,
http://docs.python.org/dev/library/collections.abc.html#collections.abc.Mapping
, is a very good one. I hadn't realized that it separated Mapping from
MutableMapping.
It would be nice if there were some examples of it's usage around,
though. I can't really tell how to use it. (E.g., it mixes in
__contains__, but it doesn't show how to specify what you are testing
for cotainment in. So it looks as if I need to specify everything
anyway because I can't tell what might be implemented in some
inappropriate way.) OTOH, it's a good list of what needs to be
implemented. (I see that I left out implementation of eq and ne, which
is pretty straightfoward, but apparently needed (unless it's
automatically being handled by the mixin...which I can't tell).
--
Charles Hixson
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: 1st Sketch at at ReadOnly dict Charles Hixson <charleshixsn@earthlink.net> - 2014-01-20 15:37 -0800
csiph-web