On 7/16/06, Greg Ewing <[EMAIL PROTECTED]> wrote: > For about the third time in my life, I thought I might > have found a use for cooperative super calls, but I've > run into another problem with the concept. > > Consider: > > class A(object): > def m(self): > print "A.m" > > class B(object): > def m(self): > print "B.m" > super(B, self).m() > > class C(B, A): > def m(self): > print "C.m" > super(C, self).m() > > >>> c = C() > >>> c.m() > C.m > B.m > A.m > > Okay so far, but... what if I want to use class B on > its own, or in combination with other classes that don't > have an m() method? > > >>> b = B() > >>> b.m() > B.m > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File "<stdin>", line 4, in m > AttributeError: 'super' object has no attribute 'm' > > In general, how is one supposed to use super() in a > class which may or may not be at the end of the mro > with respect to a particular method?
One isn't. In the world where cooperative multiple inheritance originated (C++), this would be a static error. You can only use super when you're overriding a method, not when you're defining a new method. To do what you want to do in such a world, you'd have to create a base class with a dummy implementation and inherit from that. You can do that in Python, too. > The only thing I can think of is to write all my > super()-using methods defensively like this: > > def m(self): > ... > s = super(C, self) > if hasattr(s, 'm'): > s.m() > > which seems awfully tedious. Definitely don't that. -- --Guido van Rossum (home page: http://www.python.org/~guido/) _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com