Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36909
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.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 | <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; 'defaults': 0.05; 'behave': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'typeerror:': 0.09; 'def': 0.10; '2)]': 0.16; '__getitem__,': 0.16; 'a()': 0.16; 'b()': 0.16; 'impractical': 0.16; 'instantiate': 0.16; 'key):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:class': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'jan': 0.18; '>>>': 0.18; '"",': 0.22; 'pass': 0.25; 'header :User-Agent:1': 0.26; '(most': 0.27; '[1]': 0.27; 'header:X -Complaints-To:1': 0.28; '+0100,': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'clear': 0.35; 'received:org': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'advice': 0.39; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'header:Received:5': 0.40; '2013': 0.84; 'florian': 0.84; 'notion': 0.84; 'subject:Using': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Using inner dict as class interface |
| Date | Wed, 16 Jan 2013 18:34:42 +0100 |
| Organization | None |
| References | <mailman.571.1358347369.2939.python-list@python.org> <50f6bf35$0$30003$c3e8da3$5496439d@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508490d8.dip.t-dialin.net |
| 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 | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.578.1358357701.2939.python-list@python.org> (permalink) |
| Lines | 54 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1358357701 news.xs4all.nl 6981 [2001:888:2000:d::a6]:44422 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:36909 |
Show key headers only | View raw
Steven D'Aprano wrote:
> On Wed, 16 Jan 2013 15:42:42 +0100, Florian Lindner wrote:
>
>> Hello,
>>
>> I have a:
>>
>> class C:
>> def __init__(self):
>> d = dict_like_object_created_somewhere_else()
>>
>> def some_other_methods(self):
>> pass
>>
>>
>> class C should behave like a it was the dict d.
>
> Then make it a dict:
>
> class C(dict):
> def some_other_methods(self):
> pass
>
> my_dict = C(key="value") # or C({"key": "value"})
> print len(my_dict)
> print my_dict['key']
> my_dict.some_other_methods()
If for some reason it is impractical to follow Steven's advice you can
subclass collections.Mapping or collections.MutableMapping. That should give
you a clear notion of the required methods and has defaults for some of
them.
>>> class A(Mapping): pass
...
>>> A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class A with abstract methods
__getitem__, __iter__, __len__
>>> class B(Mapping):
... def __getitem__(self, key):
... return {1:2}[key]
... def __len__(self): return 1
... def __iter__(self): yield 1
...
>>> b = B()
>>> list(b)
[1]
>>> b.items()
[(1, 2)]
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Using inner dict as class interface Florian Lindner <mailinglists@xgm.de> - 2013-01-16 15:42 +0100
Re: Using inner dict as class interface Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-16 14:54 +0000
Re: Using inner dict as class interface Peter Otten <__peter__@web.de> - 2013-01-16 18:34 +0100
csiph-web