In Python, how do you change an instantiated object after a reload?

前端 未结 7 1594
南方客
南方客 2021-02-04 06:25

Let\'s say you have an object that was instantiated from a class inside a module. Now, you reload that module. The next thing you\'d like to do is make that reload affect that c

7条回答
  •  臣服心动
    2021-02-04 07:16

    You have to make a new object. There's no way to magically update the existing objects.

    Read the reload builtin documentation - it is very clear. Here's the last paragraph:

    If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.

    There are other caveats in the documentation, so you really should read it, and consider alternatives. Maybe you want to start a new question with why you want to use reload and ask for other ways of achieving the same thing.

提交回复
热议问题