常见的魔法方法及使用

常见的魔法方法有如下:

__init__   初始化类属性的方法
__repr__   定义 当 repr()  被你的一个类的实例调用时所要产生的行为。 str() 和 repr() 的主要区别是其目标群体。 repr() 返回的是机器可读的输出,而 str() 返回的是人类可读的。
__str__    定义当 str() 被你的一个类的实例调用时所要产生的行为。后面例子中print输出的时候就相当于在调用方法__str__
__call__ 使一个类的实例像函数一样被调用:x(a, b) 调用 x.__call__(a, b), c1() 使实例可被调用

__init__方法示例代码:

 1 class Rectangle:
 2     def __init__(self,width,height):
 3         self.width = width
 4         self.height = height
 5 
 6 
 7 C1=Rectangle(3,4)
 8 
 9 #输出结果:
10 >>> C1.width
11 3
12 >>> C1.height
13 4
14 >>> 

__repr__方法示例代码:

 1 class Rectangle:
 2      def __init__(self,width,height):
 3          self.width = width
 4          self.height = height
 5      
 6  
 7      def __str__(self):
 8          
 9          return '宽度为%s,高度为%s'%(self.width,self.height)
10  
11  
12      def __repr__(self):
13           
14          return '面积为%s' % self.area()
15      
16      def area(self):
17          
18          return self.width*self.height
19  
20  C1=Rectangle(3,4)
21  
22  #输出结果:
23  >>> C1
24 面积为12
25  >>> C1.area()
26  12
27  >>> C1.__repr__
28  <bound method Rectangle.__repr__ of 面积为12>
29  >>> C1.__repr__()
30  '面积为12'
31  >>> 

__str__方法示例代码:

 1 class Rectangle:
 2     def __init__(self,width,height):
 3         self.width = width
 4         self.height = height
 5 
 6 
 7 C1=Rectangle(3,4)
 8 
 9 #输出结果:
10 >>> C1
11 <__main__.Rectangle object at 0x0000000002B38550>
12 >>> print(C1)
13 <__main__.Rectangle object at 0x0000000002B38550>
14 
15 >>> 
16 >>> C1.__str__
17 <method-wrapper '__str__' of Rectangle object at 0x0000000002B38550>
18 >>> C1.__str__()
19 '<__main__.Rectangle object at 0x0000000002B38550>'
20 >>> 
21 
22 #改写__str__方法后
23 class Rectangle:
24     def __init__(self,width,height):
25         self.width = width
26         self.height = height
27 
28     def __str__(self):
29         
30         return '宽度为%s,高度为%s'%(self.width,self.height)
31     
32 C1=Rectangle(3,4)
33 
34 #输出结果:
35 >>> C1
36 <__main__.Rectangle object at 0x0000000002B384E0>
37 >>> C1.width
38 3
39 >>> C1.height
40 4
41 >>> print(C1)
42 宽度为3,高度为4
43 >>> 

改写__str__方法后让返回值以为指定的模式输出,这里用 return '宽度为%s,高度为%s'%(self.width,self.height)返回一个字符串形式

__call__方法代码:

 1 class Rectangle:
 2     def __init__(self,width,height):
 3         self.width = width
 4         self.height = height
 5     
 6 
 7     def __str__(self):
 8         
 9         return '宽度为%s,高度为%s'%(self.width,self.height)
10 
11 
12     def __repr__(self):
13          
14         return '面积为%s' % self.area()
15     
16     def area(self):
17         
18         return self.width*self.height
19 
20 C1=Rectangle(3,4)
21 
22 #输出结果:
23 >>> C1
24 面积为12
25 >>> C1()
26 Traceback (most recent call last):
27   File "<pyshell#92>", line 1, in <module>
28     C1()
29 TypeError: 'Rectangle' object is not callable
30 >>> 

改写__call__方法

 1 class Rectangle:
 2     def __init__(self,width,height):
 3         self.width = width
 4         self.height = height
 5     
 6 
 7     def __str__(self):
 8         
 9         return '宽度为%s,高度为%s'%(self.width,self.height)
10 
11 
12     def __repr__(self):
13          
14         return '面积为%s' % self.area()
15     
16     def __call__(self):
17         
18         return '测试这个类是否可调用'
19     
20     def area(self):
21         
22         return self.width*self.height
23 
24 C1=Rectangle(3,4)
25 
26 #输出结果:
27 >>> C1
28 面积为12
29 >>> C1()
30 '测试这个类是否可调用'
31 >>> 

 __add__方法代码示例:

 1 class Rectangle:
 2     def __init__(self,width,height):
 3         self.width = width
 4         self.height = height
 5     def __str__(self):
 6         return '宽为%s,高为%s' % (self.width,self.height)
 7     def __repr__(self):
 8         return '面积为%s' % self.area()
 9     def __call__(self):
10         return 'hahahha'
11     def area(self):
12         return self.width*self.height
13     def __add__(self,other):
14         if isinstance(other,Rectangle):
15             return self.area()+other.area()
16 
17 C1=Rectangle(3,4)
18 C2=Rectangle(5,6)
19 #输出结果:
20 >>> C1
21 面积为12
22 >>> C2
23 面积为30
24 >>> C1 + C2
25 42
26 >>> 

 其他常见的运算符魔法方法:

 1 __add__(self,other)     x+y
 2 __sub__(self,other)     x-y 
 3 __mul__(self,other)     x*y  
 4 __mod__(self,other)    x%y
 5 __iadd__(self,other)    x+=y
 6 __isub__(self,other)    x-=y 
 7 __radd__(self,other)    y+x
 8 __rsub__(self,other)     y-x 
 9 __imul__(self,other)    x*=y 
10 __imod__(self,other)    x%=y
原文地址:https://www.cnblogs.com/kindnull/p/6812225.html