Accessing parent objects

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Mar 26 06:23:54 EDT 2018


On 2018年3月26日 10:43:25 +0200, Antoon Pardon wrote:
> But did they start up cleaning the standard library yet? I'll confess
> I'm only using 3.5 but when I go through the standard library I see a
> lot of classes still using the old style of calling the parant method,
> which makes it more difficult to use in a multiple inheritance scheme.
> Those that don't inheret, don't call super themselves, which makes them
> also more difficult to use in a multiple inheritance scheme.

Ah, that's a separate question! And it's a good observation.
You are right that there is a lot of legacy code in the std lib that 
doesn't use super, and probably should. Fixing that is probably a good 
project for some kindly volunteer wanting to get into Python bug-
fixing :-)
But you can usually fix that in your own code with a simple adaptor class 
that delegates to the old-style class. See for example:
https://code.activestate.com/recipes/577721
https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
> One of the problems with this is that when you go through the mro you
> will ultimatly end up calling object.method(self, spam, eggs, cheese)
> which will thrown an exception like: AttributeError: 'super' object has
> no attribute 'method'

Why in the name of all the gods would you call super() on a method you 
aren't inheriting from a superclass???
I'm sorry if my example was inaccurate or misleading, but please be 
reasonable. Calling super in a method which you aren't overloading is not 
a reasonable thing to do.
> So I find your example misleading. It is just one step, and you need a
> carefully designed hierarchy to make it work correctly and incorporating
> standard library classe into that hierarchy is not self-evident.

You *always* need a carefully designed hierarchy.
super or no super, single inheritance or delegation or multiple 
inheritance, you can't expect to just randomly throw classes together in 
a slap-dash manner and have them work.
-- 
Steve


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /