Using inner dict as class interface

Discussion in 'Python' started by Florian Lindner, Jan 16, 2013.

  1. 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
    Florian Lindner, Jan 16, 2013
    #1
    1. Advertising

  2. 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()



    --
    Steven
    Steven D'Aprano, Jan 16, 2013
    #2
    1. Advertising

  3. Florian Lindner

    Peter Otten Guest

    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)]
    Peter Otten, Jan 16, 2013
    #3
    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,708
    Thomas Weidenfeller
    Oct 12, 2005
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,010
    Alex Martelli
    Oct 19, 2003
  3. Pyenos
    Replies:
    2
    Views:
    374
    Pyenos
    Dec 27, 2006
  4. Matt Jones
    Replies:
    0
    Views:
    137
    Matt Jones
    Jan 16, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    123
    Dave Angel
    Jan 16, 2013
Loading...

Share This Page