魔术方法

魔术方法

    __str__  : 使用print打印时调用的方法

        一个小例子

    __repr__:交互模式下调试时调用的方法

        一个小例子(注意这个例子是在ipython中完成的)

 

    __call__: 让类可以像函数一样被使用

 

以下魔法方法为了解即可,不常用


类名 . __bases__  查看当前类的父类

 

类名 . __doc__  or  实例名.__doc__ 返回当前类中的说明文档(''' '''三重引号包含的内容)

 

类名 . __dict__  以字典形式返回当前类中的所有属性

 

实例名 . __class__()   查看当前实例化的类的原始类名


def __add__(self,other)    实例化类1 + 实例化类2  时运行的参数

def __sub__(self,other)    实例化类1  - 实例化类2  时运行的参数

def __mul__(self,other)    实例化类1  实例化类2  时运行的参数

def __mod__(self,other)   实例化类1 % 实例化类2  时运行的参数

def __iadd__(self,other)    实例化类1 += 实例化类2  时运行的参数

def __isub__(self,other)    实例化类1  -= 实例化类2  时运行的参数

def __imod__(self,other)   实例化类1 %= 实例化类2  时运行的参数

def __imul__(self,other)    实例化类1  *= 实例化类2  时运行的参数

def __radd__(self,other)    实例化类2  + 实例化类1  时运行的参数

def __rsub__(self,other)   实例化类2  实例化类1  时运行的参数

    一个小例子

 

原文地址:https://www.cnblogs.com/shijin7/p/9130493.html