0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonのselfについてメモ

Posted at
hello.py
class HelloForEver:
    def __init__(self, num):
        #構造体(クラス)の中のnにnという変数を定義している。(プロパティ)
        #nがメンバ(プロパティ)
        self.num = num
    def readline(self):
        if self.num > 0:
            return (self.num*8)
#インスタンスを生成
f = HelloForEver(3)
print(f.readline())

"self"にはコンストラクタで生成したインスタンスを渡している。
ex.
f = HelloForEver(3)
とすると、
self = f
となり、
class内の
self.numf.numになる。
これによって、インスタンスごとにプロパティ(Cでいうメンバを生成している)

参考文献:
https://utokyo-ipp.github.io/

0
1
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?