#topicpath /////////////////////////////////////////////////////////////////////////////// * 目次 [#g772bcf8] #contents(); /////////////////////////////////////////////////////////////////////////////// * class 定義 [#a756aa4f] class <class-name>: def __init__(self): # 初期化メソッド self.value = 0; print( "Initialize done..." ); def hoge(self, fuga): ... - ''各クラスメソッドの第1引数には、必ず self を入れること。'' - ''"self" とは、クラス内で参照される、クラス自身のインスタンス。C++ の this のようなもの。'' - アトリビュート(C++ で言うところのメンバ変数)を用意するときは、 __init__() 関数内で初期化する。宣言はない。 /////////////////////////////////////////////////////////////////////////////// * 演算子の定義 [#x3835e81] class CHoge: def __init__(self): self.x = 0; self.y = 0; # + 演算子の定義 def __add__(self, other): return CHoge( self.x + other.x, self.y + other.y ); //============================================================================== ** 各種演算子を定義する特別関数一覧 [#c5ad8ab5] |~Operator |~Special method | |self + other |__add__(self, other)| |self - other |__sub__(self, other)| |self * other |__mul__(self, other)| |self / other |__div__(self, other) or __truediv__(self,other) if __future__.division is active.| |self // other |__floordiv__(self, other)| |self % other |__mod__(self, other)| |divmod(self,other) |__divmod__(self, other)| |self ** other |__pow__(self, other)| |self & other |__and__(self, other)| |self ^ other |__xor__(self, other)| |self | other |__or__(self, other)| |self << other |__lshift__(self, other)| |self >> other |__rshift__(self, other)| |bool(self) |__nonzero__(self) (used in boolean testing)| |-self |__neg__(self)| |+self |__pos__(self)| |abs(self) |__abs__(self)| |˜self |__invert__(self) (bitwise)| |self += other |__iadd__(self, other)| |self -= other |__isub__(self, other)| |self *= other |__imul__(self, other)| |self /= other |__idiv__(self, other) or __itruediv__(self,other) if __future__.division is in effect.| |self //= other |__ifloordiv__(self, other)| |self %= other |__imod__(self, other)| |self **= other |__ipow__(self, other)| |self &= other |__iand__(self, other)| |self ^= other |__ixor__(self, other)| |self |= other |__ior__(self, other)| |self <<= other |__ilshift__(self, other)| |self >>= other |__irshift__(self, other)| - 参照元:[[Python 2.6 Quick Reference>http://rgruet.free.fr/PQR26/PQR2.6.html#SpecialMethods]] /////////////////////////////////////////////////////////////////////////////// * アトリビュートの隠蔽 [#s9a4665c] - 先頭に "_" を1個付ける -- 慣習的に、「外部から直接参照・代入してはいけないもの」と見做すことになっている。~ 実際にはクラス外部から操作出来てしまう。 - 先頭に "__" を付ける -- 実際にクラス外部から参照不可になる。 class CTest2: def __init__(self,x,y): self.m_x = x; self.m_y = y; def Start(self): self._StartSub(); def _StartSub(self): print "StartSub\n"; def _PrintPropSub(self): print "[X:", self.m_x, "][Y:", self.m_y,"]"; def PrintProp(self): self._PrintPropSub(); def __PrintProp(self): print "__PrintProp()"; self._PrintPropSub(); c2 = CTest2(10,20); c2.Start(); c2.PrintProp(); c2._PrintPropSub(); # 実行時、ここではエラーにならない c2.__PrintPropSub(); # 実行時、ここでエラーになる /////////////////////////////////////////////////////////////////////////////// * クラスの継承 [#ifaeda61] - python は、class の多重継承に対応している - 書式: class SubClass(SuperClass1 [, SuperClass2][, SuperClass3]...): //============================================================================= ** メソッドのオーバーライド [override] [#u1785bdb] - python でメソッドの override を行うと、完全な上書きがされる。即ち、 override された Super class のメソッドは呼び出されず、 Sub class のメソッドのみが呼び出される。 class CSuper: def __init__( self, x ): self.__x = x; def Set( self, x ): self.__x = x; print "This class is CSuper\n" class CSub( CSuper ): # CSuper を継承したサブクラス def __init__( self, y ): self.__y = y; def Set( self, y ): self.__y = y; print "This class is CSub\n" a = CSuper(); a.Set( 10 ); # CSuper.Set() が呼び出される b = CSub(); b.Set( 20 ); # CSub.Set() が呼び出されるが、親の CSuper.Set() は一切実行されない //============================================================================= ** Super class の取得: super() [#bcf2f2f8] - Sub class のインスタンスから Super class のインスタンスを取得するには、 super() を使用する。 - 例えば、初期化メソッドで Super class の初期化メソッドを呼び出す場合、以下のようになる: class CSub( CSuper ): def __init__( self, x, y ): super().__init__( x ); # self.__y = y; - C++ などと違って、 python では Sub class から自動的に Super class のメソッドやアトリビュートが呼ばれることはない。従って、 Super class のメソッドやアトリビュートを呼び出したい時は、このように super() を使って呼び出す。