LoginSignup
2
0

More than 1 year has passed since last update.

コンストラクタとデストラクタ

Last updated at Posted at 2020-02-20
class Person(object):
    #コンストラクタ
    def __init__(self, name):
        self.name = name
        print(self.name)

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

    #デストラクタ
    def __del__(self):
        print('good bye')

person = Person('Mike')
person.say_something()
print('######')
#オブジェクトが使われなくなると自動的にデスクトラクタが呼び出されますが、下記のように明示的に呼び出すこともできます。
#del person
Mike
hello.Mike
runrunrunrunrunrunrunrunrunrun
######
good bye
2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0