How to reload the code of a method of class object in Python?

后端 未结 4 2099
予麋鹿
予麋鹿 2020-12-20 00:24

I\'m find a way to reload the method of a class object at runtime,here is the example: I have define a class A firstly which lies on the file test.py.

class          


        
4条回答
  •  生来不讨喜
    2020-12-20 00:42

    I've just spend a while trying to solve this issue myself and ended up with a slightly different solution to the others suggested here. I've put it in a gist with plenty of comments, but as is the SO way I'll give a brief explantion here:

    Rather than modify individual instances of an old version of a class, or copy them into new instances of the new class, the idea is to set the methods of the old class (i.e. the attributes of the class itself not the instances of the class) to be the updated methods from the new class. It also takes care of chaining back over multiple versions of the class.

提交回复
热议问题