[Python-3000] [Fwd: features i'd like [Python 3000] ... #3: fix super()] (original) (raw)

Thomas Wouters thomas at python.org
Thu Dec 7 01:44:18 CET 2006


On 12/6/06, Josiah Carlson <jcarlson at uci.edu> wrote:

"Thomas Wouters" <thomas at python.org> wrote: > On 12/6/06, Jan Grant <jan.grant at bristol.ac.uk> wrote: > > On Mon, 4 Dec 2006, Ben Wing wrote: > > > > > as a result, i imagine there's a strong urge to just hardcode the name > > > of the parent > > ^^^^^^^^^^ > > > > > -- super.meth(args) calls the superclass method `meth' > > ^^^^^^^^^^^^^^ > > > > Python supports multiple inheritance, unlike Java; the design mantra is > > "explicit is better than implicit" and "ambiguity should be an error". > > Two! The two design mantras are... > > > You forget that that's actually what super() is for. It does the right thing > in the case of MI (and every other case, in fact :-) Except for this one: >>> class foo(object): ... pass ... >>> class bar(foo): ... def method(self): ... super(bar, self).method() ... >>> bar().method() Traceback (most recent call last): File "", line 1, in ? File "", line 3, in method AttributeError: 'super' object has no attribute 'method' >>>

I'm not sure what makes you say that. There is no superclass method 'method', so 'AttributeError' seems like the Right Thing to me.

Because of this kind of thing, I do my best to never produce code that

has a diamond inheritance structure (except for object), and always use things like foo.method(self).

Which would have given you the same AttributeError.

If you want co-operative MI classes, you need to do more than just use super(): you need a baseclass that provides the 'interface', if you will, that you wish to co-operate in. For many of the standard hooks, object does this sensibly. For other methods, not so much. I personally think that this is correct behaviour; I wouldn't want 'return super(ThisClass, self).gettiem(item)' to decide to return, say, None for no apparent reason (if the typo was apparent, I wouldn't have made it :). AttributeError is the right thing.

Avoiding MI is certainly a good option, provided you're able to enforce that policy somehow.

-- Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/python-3000/attachments/20061206/57ba934a/attachment.htm



More information about the Python-3000 mailing list