Constructor and destructor

class Person(object):
    #constructor
    def __init__(self, name):
        self.name = name
        print(self.name)

    #Method
    def say_something(self):
        print('hello.{}'.format(self.name))
        self.run(10)
    def run(self, num):
        print('run' * 10)

    #Destructor
    def __del__(self):
        print('good bye')

person = Person('Mike')
person.say_something()
print('######')
#When explicitly calling a destructor
#del person
Mike
hello.Mike
runrunrunrunrunrunrunrunrunrun
######
good bye

Recommended Posts

Constructor and destructor
Destructor
Comparison of class inheritance and constructor description
About _ and __