标签:host port eth lse int list **kwargs 操作 def
通过类的绑定方法
class Person():
_instance=None
def __init__(self,port,host):
self.port=port
self.host=host
@classmethod
def get_sigoleton(cls):
import settings
if not cls._instance:
cls._instance = cls(settings.PORT, settings.HOST)
return cls._instance
s1=Person.get_sigoleton()
s2=Person.get_sigoleton()
s3=Person.get_sigoleton()
print(s1)
print(s2)
s4=Person('33306','192.168.1.1')
print(s4)
通过装饰器
def get_sigoleton(cls):
import settings
instance=cls(settings.PORT,settings.HOST)
def wrapper(*args,**kwargs):
if len(args)!=0 or len(kwargs)!=0
res=cls(*args,**kwargs)
return res
else:
return instance
return wrapper
@get_sigoleton
class Person():
def __init__(self,port,host):
self.port=port
self.host=host
# Person=get_sigoleton(Person)
s1=Person()
s2=Person()
s3=Person('33306','192.168.1')
print(s1)
print(s2)
print(s3)
通过元类
class Mymeta(type):
def __init__(self,name,bases,dic):
import settings
self._instance=self(settings.PORT,settings.HOST)
def __call__(self, *args, **kwargs):
if len(args)!=0 or len(kwargs)!=0:
obj=object.__new__(self)
obj.__init__(*args,**kwargs)
return obj
else:
return self._instance
class Person(metaclass=Mymeta):
def __init__(self,port,host):
self.port=port
self.host=host
s1=Person()
s2=Person()
s3=Person('33306','192.168.1')
print(s1)
print(s2)
print(s3)
通过模块:
def test():
pass
def test2():
pass
test()
test2()
from sigonleton import s1
from sigonleton import Person
s2=Person('33306','192.168.1')
print(s1)
print(s2)
import settings
class Person():
def __init__(self,port,host):
self.port=port
self.host=host
s1=Person(settings.PORT,settings.HOST)
sort 与 sorted 区别:
标签:host port eth lse int list **kwargs 操作 def
原文地址:https://www.cnblogs.com/lzss/p/11511242.html