Re: How to properly override the default factory of defaultdict?

2016-02-19 Thread Chris Angelico
On Sat, Feb 20, 2016 at 11:24 AM, Ian Kelly wrote: >> Look like inheriting from defaultdict is easier. I don't even have to >> override the constructor as suggested by Chris Angelico above. Thanks. > > True, although there's a faint code smell as this technically violates > the Liskov Substitutio

Re: How to properly override the default factory of defaultdict?

2016-02-19 Thread Ian Kelly
On Thu, Feb 18, 2016 at 10:41 AM, Herman wrote: > From: Ben Finney >> >> you are using the inheritance hierarchy but thwarting it by not using >> ‘super’. Instead:: >> >> super().__init__(self, default_factory, *a, **kw) >> >> and:: >> >> super().__getitem__(self, key) >> -- >> \ "

Re: How to properly override the default factory of defaultdict?

2016-02-18 Thread Herman
d = dictutil.DefaultDictWithEnhancedFactory(lambda k: k) self.assertEqual("apple", d['apple']) From: Ben Finney > > you are using the inheritance hierarchy but thwarting it by not using > ‘super’. Instead:: > > super().__init__(self, default_factory, *a, **kw) > > and:: > > super(

Re: How to properly override the default factory of defaultdict?

2016-02-15 Thread Gregory Ewing
Herman wrote: I want to pass in the key to the default_factory of defaultdict and I found that defaultdict somehow can intercept my call to dict.__getitem__(self, key), What's happening here is that defaultdict doesn't actually override __getitem__ at all. Instead, it overrides __missing__, whi

Re: How to properly override the default factory of defaultdict?

2016-02-14 Thread Steven D'Aprano
On Monday 15 February 2016 11:17, Herman wrote: > I want to pass in the key to the default_factory of defaultdict Just use a regular dict and define __missing__: class MyDefaultDict(dict): def __missing__(self, key): return "We gotcha key %r right here!" % key If you want a per-in

Re: How to properly override the default factory of defaultdict?

2016-02-14 Thread Chris Angelico
On Mon, Feb 15, 2016 at 11:17 AM, Herman wrote: > I want to pass in the key to the default_factory of defaultdict and I found > that defaultdict somehow can intercept my call to dict.__getitem__(self, > key), so my class's __getitem__ have to catch a TypeError instead instead > of KeyError. The fo

Re: How to properly override the default factory of defaultdict?

2016-02-14 Thread Ben Finney
Herman writes: > I want to pass in the key to the default_factory of defaultdict and I > found that defaultdict somehow can intercept my call to > dict.__getitem__(self, key), so my class's __getitem__ have to catch a > TypeError instead instead of KeyError. The following class is my code: I don

How to properly override the default factory of defaultdict?

2016-02-14 Thread Herman
I want to pass in the key to the default_factory of defaultdict and I found that defaultdict somehow can intercept my call to dict.__getitem__(self, key), so my class's __getitem__ have to catch a TypeError instead instead of KeyError. The following class is my code: class DefaultDictWithEnhancedF