V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cevincheung
V2EX  ›  Flask

这种语法是怎么实现的?

  •  
  •   cevincheung ·
    cevin · 2017-10-12 12:46:33 +08:00 · 4001 次点击
    这是一个创建于 2359 天前的主题,其中的信息可能已经有所发展或是发生改变。
    model.query.filter(model.c.filed1 > model.c.cfiled2).all()
    

    model.c.field1 > model.c.field2 这不是个比较语句么?不是会把结果当成参数传进去么?

    18 条回复    2017-10-13 15:42:03 +08:00
    Morriaty
        1
    Morriaty  
       2017-10-12 12:53:15 +08:00
    def __gt__(self, other):
    if self.data > other.data:
    return "condition_string_one"
    else:
    return "two"
    cevincheung
        2
    cevincheung  
    OP
       2017-10-12 12:54:12 +08:00
    @Morriaty #1

    所以像其他的等于、数组、json 都是这样?
    guoziyan
        3
    guoziyan  
       2017-10-12 13:19:21 +08:00
    scala 会认为是 lambda python 也是这么处理的吗
    jakes
        4
    jakes  
       2017-10-12 13:44:51 +08:00 via iPhone
    操作符重载
    xmcp
        5
    xmcp  
       2017-10-12 13:45:52 +08:00 via iPhone
    这应该是这个库的一个语法糖。让__lt__返回一个奇怪的对象传到 filter 里。
    jyf
        6
    jyf  
       2017-10-12 14:38:47 +08:00
    python 的 orm 库里好多这种 都是生成器把戏 这个会重载生成器 然后分析你原始的输入 替换成相应的 sql
    raiz
        7
    raiz  
       2017-10-12 17:00:38 +08:00
    应该是重载运算符,返回一个 callable 对象,filter 函数里回调这个方法吧
    northisland
        8
    northisland  
       2017-10-12 17:31:52 +08:00
    一直觉得 numpy 是个比较神的库

    竟然有
    mat_a[...] = mat_b[:, 1, :, :] 这种操作。谁知道这种运算符是咋搞出来的?没时间查源码。
    ToughGuy
        9
    ToughGuy  
       2017-10-12 17:48:48 +08:00
    这不就登录传一个 bool 值进去么, 有神码奇怪的。
    ToughGuy
        10
    ToughGuy  
       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
    ToughGuy
        11
    ToughGuy  
       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
    xmcp
        12
    xmcp  
       2017-10-12 18:08:04 +08:00
    @ToughGuy 不好意思,这还真不是一个 bool 值,那行代码的作用是按照那两列的大小关系筛选数据库
    linuxchild
        13
    linuxchild  
       2017-10-12 19:03:28 +08:00
    重构了吧 - -
    CSM
        14
    CSM  
       2017-10-12 19:26:57 +08:00   ❤️ 1
    @northisland Python 里的 `...` 是 `ellipsis` 的实例,又名 `Ellipsis`:
    >>> ...
    Ellipsis
    >>> ... is Ellipsis
    True
    >>> type(Ellipsis)
    <class 'ellipsis'>

    而 `mat_b[:, 1, :, :] ` 就是个多维切片嘛。。。
    yonka
        15
    yonka  
       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))
    weifding
        16
    weifding  
       2017-10-13 08:21:51 +08:00
    就是个匿名函数。
    msg7086
        17
    msg7086  
       2017-10-13 08:33:49 +08:00
    大致可以理解成 model.c.filed1.__gt__(model.c.cfiled2) 。
    DSL 里比较常用的技巧,重载各种运算符然后返回各种构造器并且内部构建语法树。
    ToughGuy
        18
    ToughGuy  
       2017-10-13 15:42:03 +08:00
    @xmcp

    被表象迷惑了, 哈哈哈哈。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1386 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:40 · PVG 07:40 · LAX 16:40 · JFK 19:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.