2

I am programming in Python 2.7. In other languages like Ruby, there is an invocation of super in a method such that one does not need to specify the parameters with which to call super -- super. One omits the parameters in one's super call and the given super method is invoked with the same parameters as one's given method was called.

Here is an example from the StackOverflow question Super keyword in Ruby:

class Foo
  def baz(str)
    p 'parent with ' + str
  end
end

class Bar < Foo
  def baz(str)
    super
    p 'child with ' + str
  end
end

Bar.new.baz('test') # => 'parent with test' \ 'child with test'

As you can see the method baz in the class Bar contains the invocation of super without any parameters and the super method baz in the super class Foo implicitly gets called with the same parameter(s) as the method baz in the child class Bar

For a variety of reasons this is very convenient, and while obviously implicit behavior in programming has its drawbacks, it is really useful to be able to have an __init__ method in a child class that does stuff in that child class and then invokes __init__ via super without having to know the parameters for __init__ in the super class.

Please help, any advice would be useful. I have been searching for an answer and unable to find one. I have seen many questions and answers on StackOverflow about super and super in __init__ and super in Python, but nonw about whether there is syntax for calling super that does not require parameters to be specified and calls it with the same parameters as the given method was called in the child class.

What I am trying to do is very similar to raise in a try, except, raise construct in Python:

try:
    linux_interaction()
except Exception as error:
    logger.critical(str(error))
    raise

In the above block I catch all exceptions, I log any exception as critical and then I throw it with just a call to raise, no parameters. That calling syntax of raise with no parameters causes the exception that was caught in the variable named error to be thrown and preserves the calling stack/stack trace. If one called raise error instead of just raise, then the stack trace from where the error originated would be lost.

I am trying to do something similar here, just invoke super and get the super method invoked with the same parameters as the given method without needing to know what they are and without needing to specify them -- that would be convenient, right?

Anyone know if this is possible and if so what the syntax in Python 2.7 would be?

  • 2
    No, you can't do this in Python. `super()` is just a way of finding and binding a method from another class in the MRO, and from there it is just a normal function call, just like any other. – Martijn Pieters Mar 22 '19 at 22:30
  • 2
    Write a wrapper function, then apply it with a decorator? You have to hard-code the current class name regardless. – o11c Mar 22 '19 at 22:30
  • 2
    The short answer is: no, you need to provide arguments to `super` in Python 2. Python's `super` is, as far as I can tell, far more general than the Ruby keyword; it was designed to support multiple inheritance, which Ruby does not support. – chepner Mar 22 '19 at 22:30
  • 2
    I tried throwing something together, but 1. it's impossible to name the current class before its definition, so it is also required to use a class decorator and metaclass, 2. `inspect.Signature` doesn't exist in python2 and I'm too lazy to look for the backport, and 3. even in python3 `__class__` isn't usable at class-scope, only at method scope, so a metaclass is probably the only reasonable approach. – o11c Mar 22 '19 at 22:55

0 Answers0