Velocity Reviews

Velocity Reviews (
-   Python (
-   -   Re: Using inner dict as class interface (

Dave Angel 01-16-2013 03:05 PM

Re: Using inner dict as class interface
On 01/16/2013 09:42 AM, 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. So I could do:

Is it a specific requirement that the class NOT be derived from dict?
Are you trying to look like a dict, but with a few extra features? Or
must you have a dict somewhere else (singleton ??!) that you're trying
to tie this to as a proxy.

Assuming you really have to tie this to some other dict, the first thing
you need to do is save d, perhaps as a line like:

self.d = dict_like_ob....

> 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?)


Because you're duck-typing, you don't need them all, just the ones your
user will need.

> to access the inner dict or is
> there something more slick?

The more slick is to derive from dict.


All times are GMT. The time now is 08:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.