lundi 3 août 2020

Python reflection, getter to methods

class A:
    def __init__(self):
        self.var = 10
    def print(self):
        print("hiiiiii")

For an object of A, we can easily access the attributes with getattr and setattr. Is there any way in Python to have an instance of class and a method name and call it indirectly, like:

MagicalMethod( A(), "print" )

then prints "hiiiiii" for me.





Aucun commentaire:

Enregistrer un commentaire