类的内置特殊函数列表
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)