[Python] When are variables created? When will class instances be erased?

Variables are created when the file is imported.

An instance of the class is destroyed when the function finishes.

class A:
    def __init__(self):
        self.a = 'a'
        self.b = 'b'

    def __del__(self):
        print(self.__dict__)
        
def main():
    a = A()
    
main()
-> {'a': 'a', 'b': 'b'}

__del__ is executed when the class instance is destroyed. (main () is executed, and when the process is finished, the instance is destroyed and print is executed.)

Recommended Posts

[Python] When are variables created? When will class instances be erased?
When will the default arguments be bound in python? When variables are bound in closure lazy evaluation.
Python: Class and instance variables
Python class variables and instance variables
Landmines hidden in Python class variables
When will mmap (2) files be updated? (2)
When will mmap (2) files be updated? (1)
[Python] Be careful when using print
[Python] Inherit a class with class variables
[Python] Use and and or when creating variables
Predict when the ISS will be visible