LoginSignup
4
4

More than 3 years have passed since last update.

Python:クラス変数とインスタンス変数

Last updated at Posted at 2020-04-22

クラス変数とは、そのクラス共通の変数で、
インスタンス変数とは、そのインスタンスだけで使う変数です。
初学者覚書です。

class enemy:
    # クラス変数
    # インスタンスオブジェクト間で共有する変数
    ENEMY_MAX = 100  # 敵の数最大値
    CHARACTER = 'O' # 敵の表示キャラクタ

    def __init__(self,x,y):
    # インスタンス変数
    # インスタンスオブジェクト間で共有しない変数
        self.alive = True # 敵生きているフラグ
        self.x = x        # 敵x座標
        self.y = y        # 敵y座標

確実にクラス変数にアクセスするには、クラス内、外からでも、「クラス.変数名」とします。

4
4
4

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
4
4