Re: Using inner dict as class interface

Discussion in 'Python' started by Matt Jones, Jan 16, 2013.

  1. Matt Jones

    Matt Jones Guest

    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 <>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
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Matt Jones, Jan 16, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,743
    Thomas Weidenfeller
    Oct 12, 2005
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,031
    Alex Martelli
    Oct 19, 2003
  3. Pyenos
    Replies:
    2
    Views:
    386
    Pyenos
    Dec 27, 2006
  4. Florian Lindner

    Using inner dict as class interface

    Florian Lindner, Jan 16, 2013, in forum: Python
    Replies:
    2
    Views:
    133
    Peter Otten
    Jan 16, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    129
    Dave Angel
    Jan 16, 2013
Loading...

Share This Page