想问一下Python的代码运行顺序。
就下面这段代码:
class Enum(type):
_init = {}
print("40:I'm Enum")
def __init__(self, name, base, class_dict):
super(Enum, self).__init__(name, base, class_dict)
print(id(self))
def __new__(cls, *args, **kwargs):
c = super().__new__(cls, *args, **kwargs)
print('47:enum')
print(args, kwargs)
print(id(cls), id(c))
return c
class Server(object, metaclass=Enum):
print("54:I'm Server")
def __init__(self, host, port):
self.host = host
self.port = port
def __new__(cls, *args, **kwargs):
c = super().__new__(cls)
print('61:Server')
return c
class SingleMetaClass(type):
print("66:I'm SingleMetaClass")
_dct = {}
_init = {}
def __init__(self, class_name, base, class_dict):
self._init[self] = class_dict.get('__init__', None)
super(SingleMetaClass, self).__init__(class_name, base, class_dict)
def __call__(self, *args, **kwargs):
if self._init[self] is None:
key = self
else:
key = (self, args, repr(kwargs))
if self._dct.get(key) is None:
self._dct[key] = super().__call__(*args, **kwargs)
return self._dct[key]
class Coon(metaclass=SingleMetaClass):
print("85:Coon")
def __init__(self, a, b):
print(a, b)
if __name__ == '__main__':
Server('localhost', '8000')
print(Coon(1, b=2))
print(Coon(1, b=2))
print(Coon(1, 3))
运行结果:
通过结果解释运行顺序即可,感谢。
之前看了这篇文章https://blog.csdn.net/weixin_44793743/article/details/126336444
结果还是有些问题。