1 # class Foo:
2 # pass
3 # f=Foo()
4 # print(type(f))
5 # print(type(Foo))
6
7 # g={'x':1,'y':2}
8 # l={}
9 # exec("""
10 # global x,m
11 # x=10
12 # m=100
13 # z=3
14 # """,g,l)
15 # print(g)
16 # print(l)
17 # -------------------------
18 # 产生类得第一种方式:
19 class Chinese:
20 country='China'
21 def __init__(self,name,age):
22 self.name=name
23 self.age=age
24
25 def talk(self):
26 print('%s is talking'%self.name)
27
28 # obj=Chinese('alice',12)
29 # obj.talk()
30 # print(obj.name,obj.age)
31
32 #产生类得第二种方式:
33 class_name='Chinese'
34 class_base=(object,)
35 class_body="""
36 country='China'
37 def __init__(self,name,age):
38 self.name=name
39 self.age=age
40
41 def talk(self):
42 print('%s is talking'%self.name)
43 """
44 # class_dic={}
45 # exec(class_body,globals(),class_dic)
46 # # print(class_dic)
47 # ch = type(class_name,class_base,class_dic)
48 # obj1=ch('alice',12)
49 # print(obj1.__dict__)
50 # print(ch.__dict__)
51 # print(ch)
52 # print(obj1,obj1.name,obj1.age)
53
54 #自定义元类 控制类的行为:
55 class Mymeta(type): #元类
56 # print('--1--')
57 def __init__(self,class_name,class_bases,class_dic): # 控制类的创建
58 # print(class_name)
59 # print(class_bases)
60 # print(class_dic)
61 if not class_name.istitle():
62 raise TypeError('类名的首字母必须大写')
63 if '__doc__' not in class_dic or not class_dic['__doc__'].strip():
64 raise TypeError('必须有注释,且注释不能为空')
65
66 super(Mymeta, self).__init__(class_name,class_bases,class_dic)
67
68 def __call__(self, *args, **kwargs): # 控制类的实例化
69 # print(self)
70 # print(args)
71 # print(kwargs)
72 obj=object.__new__(self) #1.造空对象
73 self.__init__(obj,*args,**kwargs) #2.初始化obj
74 return obj #返回obj
75
76
77 class Chinese(object,metaclass=Mymeta):
78 '''
79 中文人的类
80 '''
81 country='China'
82 def __init__(self,name,age):
83 print('--2--')
84 self.name=name
85 self.age=age
86
87 def talk(self):
88 print('%s is talking'%self.name)
89
90 def __call__(self, *args, **kwargs):
91 print(self)
92 print(args)
93 print(kwargs)
94
95
96 # obj=Chinese('alice',12) # 触发它的类的 __call__() 元类
97 # obj(1,2,3,a=1,b=2) # 触发它的类的 __call__() Chinese
98 # print(obj.__dict__)
99 # print(Chinese.__dict__)
100 #
101 # print(type(Chinese))
102 # Chinese=Mymeta(class_name,class_bases,class_dir)
103 # print(obj.__dict__)
104
105 #自定义元类的应用:
106
107 # obj1=int(1)
108 # obj2=int(1)
109 # print(id(obj1))
110 # print(id(obj2))
111 # print(obj1 is obj2) # 实现了单例模式 参数相同 多个对象 使用同一块内存 是一种优化策略
112
113 # 单例模式 实现方式一:
114
115 class Mymeta(type):
116 def __init__(self,class_name,class_bases,class_dic):
117 super().__init__(class_name,class_bases,class_dic)
118 self.__instance=None
119
120 def __call__(self, *args, **kwargs):
121 if not self.__instance:
122 obj = object.__new__(self)
123 self.__init__(obj)
124 self.__instance=obj
125 return self.__instance
126
127
128
129 class Mysql(object,metaclass=Mymeta):
130 # __instance=None
131 def __init__(self):
132 self.host='127.0.0.1'
133 self.port=3306
134
135 # @classmethod
136 # def singleton(cls):
137 # if not cls.__instance:
138 # obj=cls()
139 # cls.__instance=obj
140 # return cls.__instance
141
142 def conn(self):
143 pass
144
145 def execute(self):
146 pass
147
148 # obj1=Mysql()
149 # obj2=Mysql()
150 # print(obj2.__dict__)
151 # print(id(obj1))
152 # print(id(obj2))
153
154 # obj1=Mysql.singleton()
155 # obj2=Mysql.singleton()
156 # # print(obj1.__dict__)
157 # print(id(obj1))
158 # print(id(obj2))
159
160 # obj1=Mysql()
161 # obj2=Mysql()
162 # print(id(obj1))
163 # print(id(obj2))