简体   繁体   中英

PyCharm not recognizing new class method without reload

I think this is a really silly / dumb question, but I am using PyCharm and constantly adding methods to a class or changing the body of the methods. When I test the class by importing the .py file and initiating the class object, it doesnt seem to recognize the changes to the class.

Is there some button I need to hit to make sure that the class code is changed.

The only thing that seems to work for me is to restart PyCharm.

When you import the class, it imports it as-is at the current time. If you make changes after that, you need to import it again. In this case, you should just be able to terminate the shell, and start it again.

This is not an error, or a bug.

There many variations of the issue you have stated. One issue I have faced is having two classes in the module - one containing another's object.

eg

class y:
    @classmethod
    def f1():
        print('old')

class x:
    def __init__(self):
        self.ref_cls = y()

    def test():
        self.ref_cls.f1() # <-- "line to change"

Now if I place a breakpoint over "line to change" and want to redef f1 to print 'new' instead of 'old', I open the evaluator and add following code:

class new: # <-- if you write 'y' instead of new it does not work
    @classmethod
    def f1():
        print('new')

self.ref_cls = new

Evaluate this and then step over to confirm in the console. This works for static methods and object methods as well.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM