我有一个这样定义的类:
class Client():
def __new__(cls):
print "NEW"
return cls
def __init__(self):
print "INIT"
当我使用它时,我得到以下输出:
cl = Client()
# INIT
__new__
未被调用。为什么?
最佳答案
看完你的回答,我改进了它
class Client(object):
def __new__(cls):
print "NEW"
return super(Client, cls).__new__(cls)
def __init__(self):
print "INIT"
这样c = Client()
输出
NEW
INIT
如预期的那样。
关于python - 为什么 __new__ 没有在我的 Python 类上被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638363/