類的內置特殊函數列表
python
__init__(self[, args]) #類的構造函數 __del__(self) #類的析構函數 __repr__(self) #與eval()兼容的對象字符串表達式(用於實例重建) __str__(self) #調用str()執行的函數 __cmp__(self) #比較兩個實例,小於爲負,等於爲0,大於爲正 __hash__(self) #hash code __nonzero__(self) #self爲邏輯假,返回0;不然返回1 __getattr__(self, name) #使用self.name時調用 __setattr__(self, name, value) #使用self.name=value時調用 __delattr__(self, name) #調用del self.name時調用 __call__(self[, args]) #像使用函數同樣使用類object(args),即object.__call__(self, args)
若是對象可經過序列或字典接口訪問,則須要實現如下函數函數
__len__(self) #內置函數len()時調用 __getitem__(self, key) #self[key]時調用 __setitem__(self. key, value) #self[key] = value時調用 __delitem__(self, key) #del self[key]時調用 __getslice__(self, i, j) #self[i:j] __setslice__(self, i, j, value) #self[i:j]=value __delslice__(self, i, j) #del self[i:j]
重載運算符code
__add__(self, other) __sub__(self, other) __mul__(self, other) __div__(self, other) __mod__(self, other) __divmod__(self, other) __pow__(self, other[, modulo]) __lshift__(self, other) __rshift__(self, other) __and__(self, other) __or__(self, other) __xor__(self, other)