Python __new__解释

Python实例化对象操作过程,首先是调用__new__()方法创建实例,然后调用__init__()方法初始化对象。一般情况下,定义类时不需要显式的重写__new__()方法;但是某些情况下,比如继承immutable类型(如int, str, tuple, frozenset)时,因无法在__init__()中修改其元素,所以需要重写__new__()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import math

class Point(tuple):
#第一个参数是类型
def __new__(cls, x, y):
return tuple.__new__(cls, (x, y))

def distance(self, other):
return math.sqrt((self[0]-other[0]) ** 2 + (self[1]-other[1]) ** 2)

def __repr__(self):
return "Point({0}, {1})".format(self[0], self[1])

p = Point(1, 1)
p2 = Point(0, 0)
p3 = Point(3, 4)
print(p.distance(p2))
print(p2.distance(p3))
print(repr(p))

__new__()返回某个类的实例对象,而__init__()不返回任何值,仅做初始化操作。