How to avoid a pattern when using super (...) in Python 2.6+?

Are there any good (suitable for use in real projects) ways or reducing the template in such things

class B(A):
    def qqq(self): # 1 unwanted token "self"
        super(B, self).qqq() # 7 unwanted tokens plus 2 duplications ("B", "qqq")
        do_something()

I want it to look like this:

class B(A):
    def qqq:
        super
        do_something()

or (more realistic)

class B(A):
    @autosuper_before
    def qqq(self):
        do_something()

Is this possible in Python 2.6+ without explicit hacks?

@link super () in Python 2.x with no arguments

+5
source share
1 answer

TL; DR

As the OP said: “Is this possible in Python 2.6+ without explicit hacks?”, Answer: No

Long version

You can create a simple decorator that will call the next parent with this method. The problem is that you will not have control over the arguments you want to pass.

Edit: , autosuper, .

def autosuper(fn):
    def f(self, *args, **kw):
        cl = super(type(self), self)
        getattr(cl, fn.__name__)(*args, **kw)
        return fn(self, *args, **kw)
    return f

? Python 3.x super, !

, Python 3.x super - , , __class__, !

, , __class__, co_freevars frame f_code . super ( ), __class__ co_freevars, , . , __class__, super, - LOAD_DEFER LOAD_GLOBAL, undefined.

, hyper = super hyper ( , super).

Python Interpreter , autosuper ( __class__, Python 2.x), , .

, , , . :

  • , , .
  • unbound method ( Py3k), im_class.
  • , (, , __class__ , )
  • , OP, , .
+4

All Articles