Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103227 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2016-02-19 17:24 -0700 |
| Last post | 2016-02-19 17:24 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: How to properly override the default factory of defaultdict? Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-19 17:24 -0700
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-02-19 17:24 -0700 |
| Subject | Re: How to properly override the default factory of defaultdict? |
| Message-ID | <mailman.6.1455927922.13884.python-list@python.org> |
On Thu, Feb 18, 2016 at 10:41 AM, Herman <sorsorday@gmail.com> wrote: > From: Ben Finney <ben+python@benfinney.id.au> >> >> 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) >> -- >> \ "Those who will not reason, are bigots, those who cannot, are | >> `\ fools, and those who dare not, are slaves." —“Lord” George | >> _o__) Gordon Noel Byron | >> Ben Finney > > super does not work for defaultdict. I am using python 2.7. If I use > super(defaultdict, self).__init__(default_factory, *a, **kw), I get the > error: > > super(defaultdict, self).__init__(default_factory, *a, **kw) > TypeError: 'function' object is not iterable You're using it incorrectly. If your class is named DefaultDictWithEnhancedFactory, then the super call would be: super(DefaultDictWithEnhancedFactory, self).__init__(default_factory, *a, **kw) You pass in the current class so that super can look up the next class. If you pass defaultdict instead, super will think that it's being called *by* defaultdict and call the __init__ method on its own superclass, dict, which has a different signature. defaultdict.__init__ is effectively skipped. > 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 Substitution Principle; the default_factory attribute on defaultdict instances is expected to be a function of zero arguments, not one.
Back to top | Article view | comp.lang.python
csiph-web