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

后端 未结 4 2302
予麋鹿
予麋鹿 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:40

    To relaod your module in the interactive interpreter, you can use

    import test
    reload(test)
    from test import A
    

    But this won't affect instance of A which already exist -- they will still be of the old type A, hence having the old method. I think it is not possible to change existing instances, nor do I think it is a good idea.

    By the way, I would recommend using IPython for code testing. It facilitates recursive reload using the %reload magic, and (even more useful) the %run magic to test modules.

提交回复
热议问题