LoginSignup
1
0

More than 1 year has passed since last update.

Pythonの初期化処理

Last updated at Posted at 2022-04-15

__init__メソッド

 これはクラスからインスタンスを生成するために使用されるメソッドで、クラスの初期化の処理を担っています。
 Rubyで例えるとinitializeメソッドと同じで次のように記述するはずです。

class クラス名
  def initialize(a,b,c)
       @a = a
       @b = b
       @c = c
  end
end

一方Pythonでは以下のように記述します。

class クラス名:
  def __init__(self,a,b,c):
    self.a = a
    self.b = b
    self.c = c

と記述します。
これによりクラスのインスタンスを生成され、a,b,cの値は初期で使用される値となります。
pythonでは__init__メソッドの引数の中にselfが入っていますが、これによりクラスのインスタンスを生成しているので、

def __init__(self, ~)

と覚えておくとよいかもしれません。

1
0
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
1
0