例如一个类
class Foo(object):
def __init__(self):
print(1)
def __new__(self):
print(2)
#2
class Foo(object):
def __init__(self):
print(self)
print(1)
def __new__(self):
print(2)
return 2
class Foo(object):
def __init__(self):
print(self)
print(1)
def __new__(cls):
print(2)
return object.__new__(cls)
'''
2
<__main__.Foo object at 0x000000000213C278>
1
'''
当我们想要一个具有参数的的新类的时候
class Demo(object):
def __init__(self,name):
self.name = name
def __new__(cls, *args, **kwargs):
return object.__new__(cls)