Using inner dict as class interface
matt.walker.jones at gmail.com
Wed Jan 16 15:58:21 CET 2013
Explicit is better than implicit. Define the dunder methods so you know
exactly what your class is doing when being indexed. You only need
__getitem__ and __setitem__ really, but if you want to treat it just like a
dict you'll need __delitem__, __len__, __iter__, __contains__ as well.
On Wed, Jan 16, 2013 at 8:42 AM, Florian Lindner <mailinglists at xgm.de>wrote:
> I have a:
> class C:
> def __init__(self):
> d = dict_like_object_created_somewhere_else()
> def some_other_methods(self):
> class C should behave like a it was the dict d. So I could do:
> c = C()
> print c["key"]
> print len(c)
> but also
> How can I achieve that? Do I need to define all methods like
> __getitem__, __len__, ... (what else?) to access the inner dict or is
> there something more slick?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list