Python Forum
PyCharm Error? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: PyCharm Error? (/thread-35040.html)



PyCharm Error? - coder_sw99 - Sep-24-2021

I have a problem with PyCharm. The Desctructor Method gets initialized, without it even been called. If I try it in the Python IDLE the intepreter doesn´t iniatialize the method, but it in PyCharm it does do it

class Vehicle:
    def __init__(self, nme, ve):
        self.name = nme
        self.velocity = ve
    def __del__(self):
        print("Object", self.name, "got deleted")
    def beschleunigen(self, value):
        self.velocity += value
        self.output()
    def output(self):
        print(self.name, self.velocity, "km/h")

# Objekte der Klasse erzeugen

car1 = Vehicle("Ford",40)
car2 = Vehicle("BMW", 45)

# Objekte betrachten
car1.output()
car2.output()
Output:
Ford 40 km/h BMW 45 km/h Object Ford got deleted Object BMW got deleted
Can somebody help me?


RE: PyCharm Error? - Yoriz - Sep-24-2021

The references to objects are deleted when the program ends.


RE: PyCharm Error? - coder_sw99 - Sep-24-2021

(Sep-24-2021, 05:51 PM)Yoriz Wrote: The references to objects are deleted when the program ends.

But why does this not happen in IDLE?


RE: PyCharm Error? - deanhystad - Sep-24-2021

Your problem is actually with IDLE. Pycharm runs the program correctly. Objects should get deleted when the program reaches end of execution. In IDLE the program never really ends. This is why you can "run" your program in IDLE and still see variables after the program is finished.

You see this same kind of thing when writing GUI programs. Most GUI toolkits have some sort of mainloop() function that you call at the end of the program that keeps the program running. Forget the mainloop() and the all you see when you run the program is a blink of the windows appearing when created and disappearing when the program ends. Run the same program in IDLE and the windows stay open.


RE: PyCharm Error? - Yoriz - Sep-24-2021

IDLE is written in python and is probably keeping a reference to the objects for longer so they don't show up as being garbage collected.

I think it might be more accurate to say IDLE is written in tkinter and that is what is holding a reference to the objects in memory.