LoginSignup
0
0

pythonのclassとか__init__とか

Posted at

参考

>>> class Human:
...     name = 'andi'
...     def say(self):
...             print('こんにちは!')
...     def friend(self):
...             print('judi')
...
>>> i = human()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'human' is not defined. Did you mean: 'Human'?
>>> i = Human()
>>> i.say()
こんにちは!
>>> i.friend()
judi

追加も可能

>>> i.height
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Human' object has no attribute 'height'
>>> i.height = 178#インスタンス化してから名前を付けている
>>> i.height
178

追加がめんどくさい、、、

>>> class Human:
...     name = 'andi'
...     def say(self):
...             print('こんにちは!')
...     def friend(self):
...             print('judi')
...     def __init__(self):
...             self.height = 178
...
>>> i = Human()#インスタンス化するときに__init__が自動的に呼び出される。
>>> i.height
178

アンダーバーのやつはまだまだわからないことだらけだけど(init,del,str)すこしずつ理解します。

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