import threadingclass Test:def __new__(cls, *args, **kwargs):print("__new__:", cls.__class__.__name__)return object.__new__(cls) def __init__(self):print("__init__:", self.__class__.__name__)t = Test()
class MyInt(int):def __new__(cls, value):return int.__new__(cls, value)if None:it = MyInt(10)print(it)from enum import Enum, unique
import enum@unique
class StrEnum(Enum):OK = enum.auto(), "success"ERROR = enum.auto(), "fail"@propertydef code(self):return self.value[0]@propertydef msg(self):return self.value[1]def emsg(name, msg=None):if isinstance(name, str):return StrEnum.ERROR.code, nameelif isinstance(name, StrEnum):return name.code, name.msgreturn StrEnum.OK.code, StrEnum.OK.msgprint(StrEnum.OK.value)
print(emsg(StrEnum.ERROR))
print(emsg("hello"))
def Single(cls):instances = {}lock = threading.RLock()def get_instance(*args, **kwargs):with lock:if cls not in instances:instances[cls] = cls(*args, **kwargs)return instances[cls]return get_instanceclass Conf:def __init__(self, glb):self._glb = glb@Single
class MyConf(Conf):def __init__(self, value, glb):self.value = valueConf.__init__(self, glb)@Single
class MyConf1(Conf):def __init__(self, value, glb):self.value = valueConf.__init__(self, glb)@Single
class ConfFactory:def __init__(self, glb):self._glb = glbdef instance(self, type):if type == 1:return MyConf(10, self._glb)elif type == 2:return MyConf1(20, self._glb)if None:conf = ConfFactory(2).instance(1)print(conf.value, conf._glb)conf1 = ConfFactory(2).instance(2)print(conf1.value, conf._glb)