几乎所有的动态语言都支持成员的动态解析,一般的在解析不到成员的时候会给出一个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。

 

相关文章:

  • 2022-12-23
  • 2021-06-30
  • 2022-02-22
  • 2021-12-20
  • 2021-05-15
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
猜你喜欢
  • 2021-11-15
  • 2021-12-21
  • 2021-09-05
  • 2022-12-23
  • 2021-08-14
  • 2021-12-17
  • 2022-12-23
相关资源
相似解决方案