Using inner dict as class interface
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
Re: Using inner dict as class interface
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Using inner dict as class interface
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 mailingli...@xgm.dewrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Using inner dict as class interface
Or do what Steven said if its exactly a dict and doesn't require special management of the underlying dict. *Matt Jones* On Wed, Jan 16, 2013 at 8:58 AM, Matt Jones matt.walker.jo...@gmail.comwrote: 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 mailingli...@xgm.dewrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
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?) See http://docs.python.org/reference/datamodel.html#special-method-names 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. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Using inner dict as class interface
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)] -- http://mail.python.org/mailman/listinfo/python-list