面象物件的反射和單列模式
阿新 • • 發佈:2018-11-28
a ="普通變數a"
b ="普通變數b"
A ="普通常量A"
B ="普通常量B"
import sys
def func():
print("func,function")
def func1():
print("func1,function")
while 1:
res = input("<<<:").strip()
if res.upper() == "Q":
break
if hasattr(sys.modules[__name__],res):
object =getattr(sys.modules[__name__],res)
if callable(object):
object()
else:
print(object)
else:
print("Error")
單例模式
class Teacher:
flag = None
def __new__(cls,*args,**kwargs):
if cls.flag is None:
cls.flag = object.__new__(cls)
return cls.flag
def __init__(self,name):
self.name = name
alex1 = Teacher("alex")
alex2 = Teacher("alex")
yuan = Teacher("yuan")
print(alex1.name)
print(yuan.name)