As to why they're separate (aside from simple historical reasons): __new__ methods require a bunch of boilerplate to get right (the initial object creation, and then remembering to return the object at the end). __init__ methods, by contrast, are dead simple, since you just set whatever attributes you need to set.
In [1]: a = [1, 2, 3]; print(id(a), a) 4590340288 [1, 2, 3] # 对list实例重新初始化改变其取值为[4, 5] In [2]: a.__init__([4, 5]); print(id(a), a) 4590340288 [4, 5]
In [3]: b = (1, 2, 3); print(id(b), b) 4590557296 (1, 2, 3) # 对tuple实例尝试重新初始化并无任何效果,符合对immutable类型的行为预期 In [4]: b.__init__((4, 5)); print(id(b), b) 4590557296 (1, 2, 3)
In [95]: class PositiveTuple(tuple):
...: def __init__(self, *args, **kwargs):
...: print('get in init one, self:', id(self), self)
...: # 直接通过索引赋值的方式会报: PositiveTuple' object does not support item assignment
...: # for i, x in enumerate(self):
...: # self[i] = abs(x)
...: # 只能尝试对self整体赋值
...: self = tuple(abs(x) for x in self)
...: print('get in init two, self:', id(self), self)
...:
In [96]: t = PositiveTuple([-3, -2, 5])
get in init one, self: 4590714416 (-3, -2, 5)
get in init two, self: 4610402176 (3, 2, 5)
In [97]: print(id(t), t)
4590714416 (-3, -2, 5)
In [128]: class PositiveTuple(tuple):
...: def __new__(cls, *args, **kwargs):
...: self = super().__new__(cls, *args, **kwargs)
...: print('get in init one, self:', id(self), self)
...: # 直接通过索引赋值的方式会报: PositiveTuple' object does not support item assignment
...: # for i, x in enumerate(self):
...: # self[i] = abs(x)
...: # 只能尝试对self整体赋值
...: self = tuple(abs(x) for x in self)
...: print('get in init two, self:', id(self), self)
...: return self
...:
...:
In [129]: t = PositiveTuple([-3, -2, 5])
get in init one, self: 4621148432 (-3, -2, 5)
get in init two, self: 4611736752 (3, 2, 5)
In [130]: print(id(t), t)
4611736752 (3, 2, 5)
As to why they're separate (aside from simple historical reasons): __new__ methods require a bunch of boilerplate to get right (the initial object creation, and then remembering to return the object at the end). __init__ methods, by contrast, are dead simple, since you just set whatever attributes you need to set.
In [1]: a = [1, 2, 3]; print(id(a), a) 4590340288 [1, 2, 3] # 对list实例重新初始化改变其取值为[4, 5] In [2]: a.__init__([4, 5]); print(id(a), a) 4590340288 [4, 5]
In [3]: b = (1, 2, 3); print(id(b), b) 4590557296 (1, 2, 3) # 对tuple实例尝试重新初始化并无任何效果,符合对immutable类型的行为预期 In [4]: b.__init__((4, 5)); print(id(b), b) 4590557296 (1, 2, 3)
In [95]: class PositiveTuple(tuple):
...: def __init__(self, *args, **kwargs):
...: print('get in init one, self:', id(self), self)
...: # 直接通过索引赋值的方式会报: PositiveTuple' object does not support item assignment
...: # for i, x in enumerate(self):
...: # self[i] = abs(x)
...: # 只能尝试对self整体赋值
...: self = tuple(abs(x) for x in self)
...: print('get in init two, self:', id(self), self)
...:
In [96]: t = PositiveTuple([-3, -2, 5])
get in init one, self: 4590714416 (-3, -2, 5)
get in init two, self: 4610402176 (3, 2, 5)
In [97]: print(id(t), t)
4590714416 (-3, -2, 5)
In [128]: class PositiveTuple(tuple):
...: def __new__(cls, *args, **kwargs):
...: self = super().__new__(cls, *args, **kwargs)
...: print('get in init one, self:', id(self), self)
...: # 直接通过索引赋值的方式会报: PositiveTuple' object does not support item assignment
...: # for i, x in enumerate(self):
...: # self[i] = abs(x)
...: # 只能尝试对self整体赋值
...: self = tuple(abs(x) for x in self)
...: print('get in init two, self:', id(self), self)
...: return self
...:
...:
In [129]: t = PositiveTuple([-3, -2, 5])
get in init one, self: 4621148432 (-3, -2, 5)
get in init two, self: 4611736752 (3, 2, 5)
In [130]: print(id(t), t)
4611736752 (3, 2, 5)