1

In Python, is there a way to call a commonly named method for all instances of a class, including inheritance-related class instances? Using the example below, when c.process() is called, I would like all "process" methods for all Parent-related instances to trigger. As such, I would want the output to print "Parent process" and "ChildType1 process" for instance c, "Parent process" and "ChildType1 process" for instance d, and "Parent process" and "ChildType2 process" for instance e -- all simply by making one call (perhaps "c.process()", for example). Or this there another architecture to achieve similar means--perhaps using arrays of class instances? Thanks!

class Parent:
  def __init__(self):
    pass

  def process(self):
    print("Parent process")

class ChildType1(Parent):
  def __init__(self):
    super().__init__()

  def process(self):
    print("ChildType1 process")

class ChildType2(Parent):
  def __init__(self):
    super().__init__()

  def process(self):
    print("ChildType2 process")

c = ChildType1()
d = ChildType1()
e = ChildType2()

c.process()
rcallist
  • 21
  • 3
  • I think the better route would be to save all the objects in a list. Then iterate the list and run: ```for child in childClassList: getattr(child, process)``` – Jeff Gruenbaum Nov 04 '21 at 17:09
  • "In Python, is there a way to call a commonly named method for all instances of a class, including inheritance-related class instances?" It is *your job* to keep track of the instances you'll need to use. – juanpa.arrivillaga Nov 04 '21 at 17:10
  • Does this answer your question? [How do I call a parent class's method from a child class in Python?](https://stackoverflow.com/questions/805066/how-do-i-call-a-parent-classs-method-from-a-child-class-in-python) – Tomerikoo Nov 04 '21 at 17:33
  • Thanks--keeping track of instances using a list sounds like a decent option. That said, I was hoping there was a more utility-like way to track all instances or universally call all "common" methods, especially in a modular program where the user can build on code by defining new classes, and where I don't want the user to have to keep track of their instances. – rcallist Nov 04 '21 at 23:01

1 Answers1

0

If the child method should always call the parent method, the solution should be simply using super, similarly to what you already did in __init__.

def process(self):
    super().process()
    print("ChildType1 process")

If for some reason you want to do this without changing the implementation of process, you could iterate over the superclasses of the object and invoke the process method wherever it is defined. This is a quick untested stub of a solution:

for cls in type(c).mro():
    if hasattr(cls, "process"):
        cls.process(c)
rrobby86
  • 1,356
  • 9
  • 14
  • Thanks. I am aware of super() but was looking for a more elegant/invisible approach, and the .mro approach seems to fit that bill. With this approach, one still needs to know or keep track of all relevant instances. Is there a way to query the system for that info? – rcallist Nov 04 '21 at 23:12
  • AFAIK there is no builtin way to obtain all existing instances of a class. One straightforward solution would be to modify `__init__` to insert any created instance in a global list, but this can messy to manage; it is probably better to keep a list manually – rrobby86 Nov 05 '21 at 10:16