LoginSignup
11
14

More than 5 years have passed since last update.

インスタンスとは (python)

Posted at

背景

今回はインターン先で開発を行い、クラス設計を行っていた際に苦労したのでインスタンスについてまとめます。

インスタンスとは

インスタンスとは様々な関数や変数を持つクラスの分身のようなものでクラス内の関数や変数を使用する際に生成します。

example

sample.py
class Test:
    def __init__(self, x):
        self.x = x

    def out(self):
        print('x = {}'.format(self.x))

if __name__ == "__main__":
    test = Test(2)
    test.out()

testはTestクラスのインスタンスであり、test(インスタンス)を用いてTestクラスのout()を呼んでいます。
結果は以下の通りです。

x = 2

11
14
2

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
11
14