76日目:Pythonプログラミング
Pythonプログラミングの講義10日目。
今日の講義は、人工無脳。講義の内容をアレンジしながら受講。
- 参考テキスト
【講義内容】
- Chapter4 オブジェクト、そして人工知能へ向けての第一歩
【ワンポイント】
プロパティ
class property(fget=None, fset=None, fdel=None, doc=None)
class C: def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.")
@property デコレータ
class C: def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x
メソッド
@staticmethod
- 静的メソッドは (C.f() のよう) クラスから呼び出したり、 (C().f() のように) インスタンスから呼び出したりできます。
class C: @staticmethod def f(arg1, ...):
@classmethod
- クラスメソッドは、(C.f() のように) クラスから呼び出すことも、(C().f() のように) インスタンスから呼び出すこともできます。 インスタンスはそのクラスが何であるかを除いて無視されます。 クラスメソッドが派生クラスから呼び出される場合は、その派生クラスオブジェクトが暗黙の第一引数として渡されます。
class C: @classmethod def f(cls, arg1, ...):
インスタンスメソッド
class C: def f(self, arg1, ...):
【今日の積み上げ】
- Python基礎