LoginSignup
0
0

More than 1 year has passed since last update.

Pythonを改めてちゃんと学ぶメモ #4

Last updated at Posted at 2022-12-30

はじめに

ここは、Pythonを改めて学んでいるメモを都度書き出していきます。
※ 筆者のバックグラウンドは#1をご覧ください。

  1. https://qiita.com/hideh_hash_845/items/58241f0ce0283e457a87
  2. https://qiita.com/hideh_hash_845/items/56c7a2c5594261f380b2
  3. https://qiita.com/hideh_hash_845/items/b5e2264fc650c3e9d4b6
  4. (ここ)
  5. https://qiita.com/hideh_hash_845/items/7b4aeb514f9eb199e9c2

文法の気づき#4

クラス

他のオブジェクト指向言語で学んでいればどうってことない

  • 第一引数は「自分自身」(インスタンス自体が第一引数)
  • 引数を設定したかったら第二引数以降に設定
  • コンストラクタは __init__
class MyClass:
    def print_class(self):
        print ("hello, MyClass.")
    def print_class_with_param(self, str):
        print ("hello, MyClass, " + str + "!")

obj = MyClass()
obj.print_class()
# hello, MyClass.
obj.print_class_with_param("and Python")
# hello, MyClass, and Python!

モジュール

  • クラスを作って "MyClass.py" みたいに保存。呼び出し側は import MyClass で呼び出せる
  • モジュールの先頭で何かを宣言する必要はなさそう("package MyClass"みたいなの)
0
0
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
0