Adding a method that makes the call to an inheritance function

Viewed 33

I'm trying to add a method for a class that it'll call another method that is in a heritance class. But i'm getting an error, probably because the internal function call is readed as function and not as a method.

Below an example:

class ListItems():
   def do_something(self):
      print('doing')

class TargetClass(ListItems):
   pass

def external_function(self):
   print('i want to do something special')
   self.do_something()

setattr(TargetClass, 'external_function', classmethod(external_function))

target = TargetClass()
target.external_function()

Error:

TypeError: do_something() missing 1 required positional argument: 'self'

I know that is a kind of meta-programming but i didn't found how implement this. Is it possible in Python?

0 Answers
Related