-1

I have a problem with understanding function super() behavior in abc.ABCMeta class in python3.6.

class ParentClass():
    def test():
        return 1
​
@six.add_metaclass(ABCMeta)
class ChildClass(ParentClass):
    def test(self):
        test_ = super().test
        test_ = 2
        return test_
​
a = ChildClass()
​
a.test()

Code failed with 'TypeError: super(type, obj): obj must be an instance or subtype of type'.

When I used super(ChildClass, self).test it worked correctly! Printing from ChildClass test function:

print(__class__)

print(type(__class__))

print(ChildClass)

print(type(ChildClass))

I get next output:

<class '__main__.ChildClass'>

<class 'type'>

<class '__main__.ChildClass'>

<class 'abc.ABCMeta'>

I feel that the reason in object initialization but I can't undersand this information with my current skills in OOP and Python OOP.

  • Not an answer but it makes me wonder: It's a bit odd that you define an abstract base class that isn't actually a "base-class" but a derived class from a non-abstract base class. – MSeifert Oct 20 '19 at 17:42
  • For simplicity of understanding my problem. In reality I have `@abstractmethod` in `ChildClass` and `ChildClassTwo(ChildClass)` which defines this method. – Dmitry Kurgansky Oct 20 '19 at 17:48
  • I meant why `ParentClass` isn't an abstract base class. Also it's not really relevant, I just wondered. – MSeifert Oct 20 '19 at 17:52

1 Answers1

3

0-argument super uses the class in which the call actually appeared, but after @six.add_metaclass(ABCMeta), the class bound to the ChildClass name is a new class created by the decorator. 0-argument super is still using the original class.

If you're going to use 0-argument super, which only works on Python 3, just use Python 3 metaclass syntax instead of six.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    Just if anyone wonders: The metaclass syntax in Python 3.x in this case would be: `class ChildClass(ParentClass, metaclass=ABCMeta): ...` – MSeifert Oct 20 '19 at 17:44