Python的程序结构[1] -> 方法/Method[4] -> 魔术方法 __call__ / __str__ / __repr__

__call__ 方法


__call__ 是当对象被调用会调用的方法,允许一个对象(类的实例等)像函数一样被调用,也可以传入参数。

 1 class Foo():
 2     def __init__(self, x, y):
 3         self.x = x
 4         self.y = y
 5 
 6     def __call__(self, m, n):
 7         print('x is %s, y is %s, m is %s, n is %s' % (self.x, self.y, m, n))
 8 
 9 Foo(1, 2)(3, 4)
10 
11 f = Foo(5, 6)
12 f(7, 8)

上面的代码中首先定义了一个 Foo 类,然后定义了 __call__ 方法,此时,这个类所生成的实例便可以像函数一样被调用了。 运行代码输出以下结果,

x is 1, y is 2, m is 3, n is 4
x is 5, y is 6, m is 7, n is 8

从第 9 行和第 12 行中可以看出,实例对象 Foo(1, 2) 和 f 可以像函数一样被调用,此时会调用到内部的 __call__ 方法。

__str__ / __repr__ 方法


__str__ 方法和 __repr__ 方法的作用十分类似,分别对应了 str() 和 repr() 函数,同时 __str__ 方法也是 print() 函数执行时会显示的内容。通过对这两个特殊方法的重载可以改变其显示的内容。其中,__str__ 在 print() 函数,str() 函数,格式化字符串(%s 与 format)中会被调用,而 __repr__ 在 repr() 函数,格式化字符串(%r)等情况下会被调用。

 1 class new(int):
 2     # Reload the __str__ function, when print(or format) it, this function will be called
 3     def __str__(self):
 4         return 'Null'
 5 
 6     # Reload the __repr__ function, when repr(or %r) it, this function will be called
 7     def __repr__(self):
 8         return str(self+1)
 9     
10 n = new(7)
11 print(n)    # Null
12 print('%s' % n) # Null
13 print('{}'.format(n)) # Null
14 print('%r' % n) # 8

上面的代码中,首先创建了一个新的类,这个类继承自 int 类,也就是拥有 int 类的所有内置方法,此时我们对其内部的 __str__ 和 __repr__ 方法进行重载,定义这个新的类型 __str__ 方法返回 ‘Null’ ,而 __repr__ 方法返回原数值加一的新数值的字符串格式。

Note: 此处值得注意的是 __repr__ 中的 str() 函数调用的是 int 类的 __str__ 函数,而不是自定义的 New 类的 __str__ 函数,因此返回的是数值加一的字符串,而不会产生 ‘Null’+1 的运算错误。这是由于 New 类的 __add__ 方法继承自 int,将 self 与 1 进行相加后返回了一个 int 类型的数值

通过显示输出可以看到,对魔术方法的修改起到了相应的作用。

 

参考链接


http://www.cnblogs.com/styier/p/6111370.html

原文地址:https://www.cnblogs.com/stacklike/p/8098026.html