Skip to content

Commit 9531757

Browse files
committed
add metaclass
1 parent 49220e3 commit 9531757

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* [learn python the hardway](http://learnpythonthehardway.org/book/)
2626
* [Python官网教程](https://docs.python.org/2/tutorial/)
2727

28+
### 邮件订阅
29+
* [python weekly](http://www.pythonweekly.com/)
2830

2931
# 在sublime中运行python
3032
“Preference(首选项)”-----》“Browse Packages(浏览程序包)”----------》“python”,在打开的目录中修改Python.sublime-build文件类似如下

meta-class.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
4+
# type(name, bases, dict)
5+
# name:class 的名称
6+
# bases:class 的父类,是个 tuple 类型
7+
# dict:class 的 namespace,表现为它的属性
8+
9+
def greet(self, word='hello'):
10+
print word + ',i am ' + self.name;
11+
12+
def personInit(self, name):
13+
self.name = name;
14+
15+
Person = type('Person', (), {'type': 'anim', 'greet': greet, '__init__':personInit});
16+
17+
p = Person('joel');
18+
p.greet('hi');
19+
20+
# __metaclass__属性
21+
# 创建一个类的时候,可以给它添加 __metaclass__属性
22+
# class Foo(object):
23+
# __metaclass__ = something…
24+
# […]
25+
# 这样做,Python就会使用指定的元类创建Foo类。
26+
27+
# 自定义元类,默认是 type
28+
29+
30+
# 关于python的元类 https://medium.com/leeon-code/python-ca836e7960e2

0 commit comments

Comments
 (0)