-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeta_programming.py
More file actions
124 lines (99 loc) · 3.41 KB
/
meta_programming.py
File metadata and controls
124 lines (99 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#! /usr/bin/python
# -*- coding:utf-8 -*-
class Main(object):
_dict = dict()
def __new__(cls):
if 'key' in Main._dict:
print('Exists')
return Main._dict['key']
else:
print('New')
return object.__new__(cls)
def __init__(self):
print("Init")
Main._dict['key'] = self
class Class_method(object):
bar = 1
#@classmehtod
def class_foo(cls):
print("Hello", self)
print(cls.bar)
print(class_method.bar)
# 析构
class Simple(object):
def __init__(self):
print("constructor called, id = {id}".format(id=id(self)))
print("Hello {name} !".format(name="James"))
def __del__(self):
print("destructor called, id = {0}".format(id(self)))
def func(self):
print("new called, id={0}".format(id=id(self)))
print("Simple func!")
# super()方法 http://www.runoob.com/python/python-func-super.html
class Parent():
def __init__(self):
self.parent = "I'm the parent;"
print('Parent')
def bar(self, message):
print("{} from Parent".format(message))
class FooChild(Parent):
def __init__(self):
super(FooChild, self).__init__()
#super(FooChild,self) 首先找到FooChild的父类(即FooParent类),然后把类FooChild的对象转换为类 FooParent 的对象
print("child")
def bar(self, message):
super(FooChild, self).bar(message)
print('Child bar function')
print(self.parent)
# python元编程 http://python.jobbole.com/85721/
# new(cls, args, *kwargs) 创建对象时调用,返回当前对象的一个实例;注意:这里的第一个参数是cls即class本身
# init(self, args, *kwargs) 创建完对象后调用,对当前对象的实例的一些初始化,无返回值,即在调用new之后,根据返回的实例初始化;注意,这里的第一个参数是self即对象本身【注意和new的区别】
# call(self, args, *kwargs) 如果类实现了这个方法,相当于把这个类型的对象当作函数来使用,相当于 重载了括号运算符
class Test():
def __init__(self, *args, **kwargs):
print("init")
# super(Test, self).__init__(*args, **kwargs)
def __new__(cls, *args, **kwargs):
print("new", cls)
return super(Test, cls).__new__(cls, *args, **kwargs)
def __call__(self, *args, **kawrgs):
print("call")
# 内建函数的重写
class Pair():
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
# return 'Pair({},{})'.format(self.x, self.y)
return 'Pair(%s, %s)' % (self.x, self.y)
supper()
def __str__(self):
return '({}, {})'.format(self.x, self.y)
# 类属性的委托访问, __getattr__
class A:
def f_one(self, x):
print("1")
def f_two(self):
return("2")
class B(A):
def __init__(self):
self._a = A()
def f_three(self):
return("f_three")
def __getattr__(self, name):
return getattr(self._a, name)
if __name__ == "__main__":
# a = Simple()
# b = Simple()
# print(Class_method.bar)
# Class_method.class_foo
#main = Main()
#main_1 = Main()
#main_2 = Main()
print("----create a new object-----")
test = Test()
print("----call func-----")
test()
foochild = FooChild()
print('-------------------')
foochild.bar("HelloWorld")