Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36897 > unrolled thread
| Started by | Florian Lindner <mailinglists@xgm.de> |
|---|---|
| First post | 2013-01-16 15:42 +0100 |
| Last post | 2013-01-16 18:34 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Using inner dict as class interface Florian Lindner <mailinglists@xgm.de> - 2013-01-16 15:42 +0100
Re: Using inner dict as class interface Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-16 14:54 +0000
Re: Using inner dict as class interface Peter Otten <__peter__@web.de> - 2013-01-16 18:34 +0100
| From | Florian Lindner <mailinglists@xgm.de> |
|---|---|
| Date | 2013-01-16 15:42 +0100 |
| Subject | Using inner dict as class interface |
| Message-ID | <mailman.571.1358347369.2939.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-16 14:54 +0000 |
| Message-ID | <50f6bf35$0$30003$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #36897 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-01-16 18:34 +0100 |
| Message-ID | <mailman.578.1358357701.2939.python-list@python.org> |
| In reply to | #36900 |
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)]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web