Re: Overriding dict constructor

2010-09-21 Thread Raymond Hettinger
[Steven D'Aprano] > But if I try to create a regular dict from this, dict() doesn't call my > __getitem__ method: > > >>> dict(d) > > {0: ('a', 'extra_data'), 1: ('b', 'extra_data')} > > instead of {0: 'a', 1: 'b'} as I expected. > > How can I fix this? Try using dict(d.items()). Raymond -- ht

Re: Overriding dict constructor

2010-09-21 Thread Duncan Booth
Steven D'Aprano wrote: > On Mon, 20 Sep 2010 11:53:48 +, Duncan Booth wrote: > >> I was going to suggest overriding items() (or iteritems() for Python >> 2.x), but while that is another hole that your values leak out it isn't >> the hole used by the dict constructor. > > Yes, I already ove

Re: Overriding dict constructor

2010-09-20 Thread Hrvoje Niksic
Christian Heimes writes: > Am 20.09.2010 13:11, schrieb Steven D'Aprano: >> I have a dict subclass that associates extra data with each value of the >> key/value items: > [...] >> How can I fix this? > > Since the dict class is crucial to the overall performance of Python, > the dict class behav

Re: Overriding dict constructor

2010-09-20 Thread Steven D'Aprano
On Mon, 20 Sep 2010 11:53:48 +, Duncan Booth wrote: > I was going to suggest overriding items() (or iteritems() for Python > 2.x), but while that is another hole that your values leak out it isn't > the hole used by the dict constructor. Yes, I already override items(), keys(), values(), thei

Re: Overriding dict constructor

2010-09-20 Thread Christian Heimes
Am 20.09.2010 13:11, schrieb Steven D'Aprano: > I have a dict subclass that associates extra data with each value of the > key/value items: [...] > How can I fix this? Since the dict class is crucial to the overall performance of Python, the dict class behaves bit different than other classes. I

Re: Overriding dict constructor

2010-09-20 Thread Duncan Booth
Steven D'Aprano wrote: > I have a dict subclass that associates extra data with each value of the > key/value items: > > class MyDict(dict): > def __setitem__(self, key, value): > super(MyDict, self).__setitem__(key, (value, "extra_data")) > def __getitem__(self, key): >