3

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.

1

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I didnt realize that I needed to terminate the shell and start over there. I thought that reimporting the .py file would reload all the code. But apparently, if it was already imported, it doesnt overwrite.
You can also just reload() the class.
0

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.

e.g.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.