Re: using super



Steven D'Aprano wrote:
...
I'm not sure if this is your only problem or not, but super() only works with new-style classes, not with classic classes. You must inherit from object, or it cannot possibly work.

Change "class A" to "class A(object)".
Absolutely correct.

However, the suggested simpler code cannot work on any released Python:

def chain(meth): # A decorator for calling super.
def f(self, *args, **kwargs):
result = meth(self, *args, **kwargs)
S = super(self.__class__, self)
This line is the problem. The class parameter needs to be the class
name (B in this case) in which the chaining method is defined, not that
of the object itself.

getattr(S, meth.__name__)(*args, **kwargs)
return result
f.__name__ = "chained_" + meth.__name__
return f


class A(object):
def foo(self, x):
print "I am %s" % self
return x

class B(A):
@chain
def foo(self, x):
print "This is B!!!"
return x + 1

You'll see the problem once you figure out what goes wrong with:
class C(B):
@chain
def foo(self, x):
print "This is C!!!"
return x + 2

C().foo(5)


As to the original idea, better to give it up.
Typically code for a "chained" method "foo" that
returns a result will want to (in some way) use
the result from that call in forming its result.
Python's super allows you to place that call where
it belongs in your code (perhaps after some logging
is enabled, for example) and not just "at the spot
the guru insists the chaining happens." The recursion-
like call to the super method is placed explicitly
in Python so you can see how it works. If super is
too tough to explain, I expect single inheritance is
all you are using. simply remind people to call
A.foo(self, <args>) within the definition of foo in
B.

--Scott David Daniels
Scott.Daniels@xxxxxxx
.



Relevant Pages

  • Re: Inheritance issue...
    ... super object returned is unbound. ... but new-style classes support lots of things that old-style classes ...
    (comp.lang.python)
  • Re: using super
    ... I'm not sure if this is your only problem or not, but super() only works ... with new-style classes, not with classic classes. ... def chain: # A decorator for calling super. ...
    (comp.lang.python)
  • RE: calling super()
    ... anywhere in the inheritance tree so super() isn't going to help.. ... New-style classes are known as types, ... classobjs. ... print 'derive2 init' ...
    (comp.lang.python)
  • using super
    ... I'm trying to make a method call automatically to its super using this ... def pr: ... (had there been class decorators the syntax would have been simpler, ... Currently I pass the base class to 'chain' - ...
    (comp.lang.python)
  • Re: A question about inheritance
    ... I didn't know about new-style classes (I had learned python ... After reading about new-style classes, I find that your solution is ... using super avoids cl2 from having to know ... > If there's a chance you might have multiple inheritance at some point ...
    (comp.lang.python)