Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass function with super() when creating class dynamically?

Let’s say I have this code:

class StaticParent:
  def _print(self):
    print("I'm StaticParent")

class StaticChild(StaticParent):
  def _print(self):
    print('StaticChild saying: ')
    super()._print()

def _parent_print_proto(self):
  print("I'm DynamicParent")

def _child_print_proto(self):
  print('DynamicChild saying: ')
  super()._print()

DynamicParent = type('DynamicParent', tuple([]), {"_print": _parent_print_proto})

DynamicChild = type('DynamicChild', (DynamicParent,), {"_print": _child_print_proto})

sc = StaticChild()
sc._print()

dc = DynamicChild()
dc._print()

And it’s output is:

StaticChild saying:
I'm StaticParent
DynamicChild saying:
Traceback (most recent call last):
  File "/tmp/example.py", line 28, in <module>
    dc._print()
  File "/tmp/example.py", line 17, in _child_print_proto
    super()._print()
RuntimeError: super(): __class__ cell not found

So question is how to create prototype method for many classes that calling super()?


PS I tried to implement method with lambda but it is also not working:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

DynamicChildWithLambda = type('DynamicChild', (DynamicParent,), {"_print": lambda self : print('Lambda saying: ', super()._print())})
Traceback (most recent call last):
  File "/tmp/example.py", line 30, in <module>
    dcwl._print()
  File "/tmp/example.py", line 23, in <lambda>
    DynamicChildWithLambda = type('DynamicChild', (DynamicParent,), {"_print": lambda self : print('Lambda saying: ', super()._print())})
RuntimeError: super(): __class__ cell not found


PS2 Also I tried this way:

class StaticParent:

  def _print(self):
    print("I'm StaticParent")

def _child_print_proto(self):
  print('DynamicChild saying: ')
  super(StaticParent, self)._print()

DynamicChild = type('DynamicChild', (StaticParent,), {"_print": _child_print_proto})

dc = DynamicChild()
dc._print()
DynamicChild saying:
Traceback (most recent call last):
  File "/tmp/example.py", line 13, in <module>
    dc._print()
  File "/tmp/example.py", line 8, in _child_print_proto
    super(StaticParent, self)._print()
AttributeError: 'super' object has no attribute '_print'

>Solution :

As mentioned by Mark super arguments can’t be empty

class StaticParent:
    def _print(self):
        print("I'm StaticParent")

class StaticChild(StaticParent):
    def _print(self):
        print('StaticChild saying: ')
        super()._print()

def _parent_print_proto(self):
    print("I'm DynamicParent")

def _child_print_proto(self):
    print('DynamicChild saying: ')
    super(self.__class__, self)._print()

DynamicParent = type('DynamicParent', tuple([]), {"_print": _parent_print_proto})

DynamicChild = type('DynamicChild', (DynamicParent,), {"_print": _child_print_proto})

sc = StaticChild()
sc._print()

dc = DynamicChild()
dc._print()
StaticChild saying: 
I'm StaticParent
DynamicChild saying: 
I'm DynamicParent
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading