A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2006-July/067480.html below:

[Python-Dev] Problem with super() usage

[Python-Dev] Problem with super() usageGreg Ewing greg.ewing at canterbury.ac.nz
Sun Jul 16 10:12:11 CEST 2006
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?

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.

Does the Theory of Cooperative Method Calls have
anything to say about this?

--
Greg
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4