model.query.filter(model.c.filed1 > model.c.cfiled2).all()
model.c.field1 > model.c.field2 这不是个比较语句么?不是会把结果当成参数传进去么?
|  |      1Morriaty      2017-10-12 12:53:15 +08:00 def __gt__(self, other): if self.data > other.data: return "condition_string_one" else: return "two" | 
|  |      2cevincheung OP | 
|      3guoziyan      2017-10-12 13:19:21 +08:00 scala 会认为是 lambda python 也是这么处理的吗 | 
|  |      4jakes      2017-10-12 13:44:51 +08:00 via iPhone 操作符重载 | 
|  |      5xmcp      2017-10-12 13:45:52 +08:00 via iPhone 这应该是这个库的一个语法糖。让__lt__返回一个奇怪的对象传到 filter 里。 | 
|      6jyf      2017-10-12 14:38:47 +08:00 python 的 orm 库里好多这种 都是生成器把戏  这个会重载生成器 然后分析你原始的输入 替换成相应的 sql | 
|      7raiz      2017-10-12 17:00:38 +08:00 应该是重载运算符,返回一个 callable 对象,filter 函数里回调这个方法吧 | 
|  |      8northisland      2017-10-12 17:31:52 +08:00 一直觉得 numpy 是个比较神的库 竟然有 mat_a[...] = mat_b[:, 1, :, :] 这种操作。谁知道这种运算符是咋搞出来的?没时间查源码。 | 
|      9ToughGuy      2017-10-12 17:48:48 +08:00 这不就登录传一个 bool 值进去么, 有神码奇怪的。 | 
|      10ToughGuy      2017-10-12 17:50:06 +08:00 In [1]: x = lambda x: print(x) In [2]: x(1>2) False In [3]: x(1>0) True | 
|      11ToughGuy      2017-10-12 17:52:30 +08:00 额 上面例子匿名函数变量名和参数一样, 看起来有点奇怪。 In [1]: x = lambda y: print(y) In [2]: x(1>2) False In [3]: x(1>0) True In [4]: print(1>2) False In [5]: print(1>0) True | 
|      13linuxchild      2017-10-12 19:03:28 +08:00 重构了吧 - - | 
|  |      14CSM      2017-10-12 19:26:57 +08:00  1 @northisland Python 里的 `...` 是 `ellipsis` 的实例,又名 `Ellipsis`: >>> ... Ellipsis >>> ... is Ellipsis True >>> type(Ellipsis) <class 'ellipsis'> 而 `mat_b[:, 1, :, :] ` 就是个多维切片嘛。。。 | 
|      15yonka      2017-10-12 21:31:21 +08:00 class Field: def field_value(self, o): return getattr(o, field_name) # field_name 用 meta class 等机制得到 def __gt__(self, other): return lambda o: cmp(self.field_value(o), other.field_value(o)) | 
|      16weifding      2017-10-13 08:21:51 +08:00 就是个匿名函数。 | 
|  |      17msg7086      2017-10-13 08:33:49 +08:00 大致可以理解成 model.c.filed1.__gt__(model.c.cfiled2) 。 DSL 里比较常用的技巧,重载各种运算符然后返回各种构造器并且内部构建语法树。 |