Passing expressions to functions?

前端 未结 6 520

I\'m not quite sure what I mean here, so please bear with me..

In SQLAlchemy, it appears I\'m supposed to pass an expression to filter() in certain cases. When I try to

6条回答
  •  旧时难觅i
    2021-02-01 20:19

    You have to implement __eq__() . For example ::

    class A(object):
        def __eq__(self, other):
            return (self, '==', other)
    

    Then, for the function, which you want to get the expression, like ::

    def my_func(expr):
        # deal with the expression
        print(expr)
    
    >>> a = A()
    >>> my_func(a == 1)
    (<__main__.A object at 0x1015eb978>, '==', 1)
    

提交回复
热议问题