标签:str cal 为什么 __call__ port 四种 acl import rom
name = 'xichen'
age = 18
class Mydic:
_instance = None
def __init__(self,name,age):
self.name = name
self.age = age
@classmethod
def get_object(cls):
if not cls._instance:
cls._instance = cls(name,age)
return cls._instance
obj1 = Mydic.get_object()
print(obj1)
obj2 = Mydic.get_object()
print(obj2)
obj3 = Mydic(name = 'nick',age = 19)
print(obj3)
<main.Mydic object at 0x000001D3B8841978>
<main.Mydic object at 0x000001D3B8841978>
<main.Mydic object at 0x000001D3BF6ECD68>
name = 'xichen'
age = 18
def wrapper(func):
func._instance = func(name, age)
def inner(*args,**kwargs):
if len(args) == 0 and len(kwargs) == 0:
return func._instance
return func(*args, **kwargs)
return inner
@wrapper
class Mydic:
def __init__(self,name,age):
self.name = name
self.age = age
obj1 = Mydic()
print(obj1)
obj2 = Mydic()
print(obj2)
obj3 = Mydic(name = 'nick',age = 19)
print(obj3)
<main.Mydic object at 0x0000015AEA2C92B0>
<main.Mydic object at 0x0000015AEA2C92B0>
<main.Mydic object at 0x0000015AEA0F9E80>
name = 'xichen'
age = 18
class Mymeta(type):
def __init__(self,name,bases,dic):
self._instance=self(name, age)
def __call__(self, *args, **kwargs):
if len(args)==0 and len(kwargs)==0:
return self._instance
else:
obj=object.__new__(self)
obj.__init__(*args, **kwargs)
return obj
class Mydic(metaclass=Mymeta):
def __init__(self,port,host):
self.port=port
self.host=host
obj1=Mydic()
print(obj1)
obj2=Mydic()
print(obj2)
obj3=Mydic('nick',19)
print(obj3)
<main.Mydic object at 0x000001D58E28CDA0>
<main.Mydic object at 0x000001D58E28CDA0>
<main.Mydic object at 0x000001D58E28CC88>
# Mydic.py
import settings
class Modlers():
def __init__(self,name,age):
self.name = name
self.age = age
s1= Modlers(settings.name,settings.age)
# settings.py
name = 'xichen'
age = 18
def test():
from mydic import s1
print(s1)
def test2():
from mydic import s1 as s2
print(s2)
test()
test2()
from mydic import s1
from mydic import Modlers
s2 = Modlers('nick',19)
print(s2)
<mydic.Modlers object at 0x0000019967C8CF28>
<mydic.Modlers object at 0x0000019967C8CF28>
<mydic.Modlers object at 0x000002AAFC639128>
标签:str cal 为什么 __call__ port 四种 acl import rom
原文地址:https://www.cnblogs.com/xichenHome/p/11508222.html