Python:Hello World级别的SimpleDb

背景

几乎所有的动态语言都支持成员的动态解析,一般的在解析不到成员的时候会给出一个hook点让你自定义一些有意思的实现。.Net4之后增加了对动态类型的支持,在动态类型上就有这种机制。

模拟SimpleDb

 1 # coding = utf-8
 2 
 3 class SimpleDB:
 4     def __getattribute__(self, name):
 5         return Table(name)
 6 
 7 class Table:
 8     def __init__(self, table):
 9         self.__table = table
10 
11     def select(self, condition):
12         print('table: %s, condition: %s' % (self.__table, condition))
13 
14 test = SimpleDB()
15 test.Users.select({'name': '段光伟', 'age': 30})

注意:上面的__getattribute__就是python提供的hook。

 

posted on 2014-05-03 11:15  幸福框架  阅读(827)  评论(0编辑  收藏  举报

导航

我要啦免费统计