为什么这个python表达式参数在调用时没有扩展?

Tom*_*Tom 8 python google-app-engine arguments function parameter-passing

在google appengine NDB中有这样的查询:

query = Account.query(Account.userid >= 40)
Run Code Online (Sandbox Code Playgroud)

为什么Account.userid >= 40表达式在调用时不会在作为参数传递之前扩展为true或false?过滤器表达式如何传递给查询?是否完成了运算符重载?

Jes*_*ebb 2

Ignacio 是正确的,NDB 代码正在其Property上定义自定义魔术方法以进行比较检查。这些函数(__eq____ne____lt__等)都在幕后调用此自定义_comparison函数。

def _comparison(self, op, value):
    """Internal helper for comparison operators.
    Args:
      op: The operator ('=', '<' etc.).
    Returns:
      A FilterNode instance representing the requested comparison.
    """
    # NOTE: This is also used by query.gql().
    if not self._indexed:
      raise datastore_errors.BadFilterError(
          'Cannot query for unindexed property %s' % self._name)
    from .query import FilterNode  # Import late to avoid circular imports.
    if value is not None:
      value = self._do_validate(value)
      value = self._call_to_base_type(value)
      value = self._datastore_type(value)
    return FilterNode(self._name, op, value)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,代码不返回布尔结果,它返回一个实例,FilterNode该实例本身会适当地计算为 true/falsey 值以进行比较。

为什么Account.userid >= 40表达式在作为参数传递之前没有在调用时扩展为 true 或 false?

从技术上讲,它是在调用函数之前进行扩展/评估的query(),它只是没有评估为布尔值。