Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Using inner dict as class interface

Thread Tools

Re: Using inner dict as class interface

Matt Jones
Posts: n/a
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.

*Matt Jones*

On Wed, Jan 16, 2013 at 8:42 AM, Florian Lindner <(E-Mail Removed)>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. So I could do:
> c = C()
> print c["key"]
> print len(c)
> but also
> c.some_other_method()
> 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?
> Thanks,
> Florian
> --

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using inner dict as class interface Florian Lindner Python 2 01-16-2013 05:34 PM
Re: Using inner dict as class interface Dave Angel Python 0 01-16-2013 03:05 PM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM