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.

theStud54
  • 705
  • 1
  • 8
  • 19
  • Are you sure that [you're not in Power Save Mode](http://stackoverflow.com/q/23462940/1079354)? – Makoto Sep 30 '15 at 12:58

2 Answers2

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.

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • 1
    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. – theStud54 Sep 30 '15 at 13:10
  • 1
    You can also just reload() the class. – Andrés Marafioti Sep 30 '15 at 13:25
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.

Ulysses
  • 5,616
  • 7
  • 48
  • 84