jeudi 8 juin 2017

How to call unbound method on object of subclass in Python

I have the following classes in Python 2.7:

class Parent():
    def some_method(self):
        do_something()

class Child(Parent):
    def some_method(self):
        do_something_different()

Suppose I have a bunch of objects that I want to run some_method on. I execute the following lines (the first two are for the sake of this example):

c = Child()
m = Parent.some_method

m(c)  # do_something() gets called

Is there some construct such that in the last line do_something_different() gets called instead, without using any information about Child (as I potentially have many such classes inheriting from Parent)?





Aucun commentaire:

Enregistrer un commentaire