The Python Oracle

Why is this python expression parameter is not expanded at call time?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Why Is This Python Expression Parameter Is Not Expanded At Call Time?
00:26 Accepted Answer Score 2
01:21 Thank you

--

Full question
https://stackoverflow.com/questions/4720...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #function #googleappengine #arguments #parameterpassing

#avk47



ACCEPTED ANSWER

Score 2


Ignacio is correct, the NDB code is defining custom magic methods on its Property class for comparison checking. These functions (__eq__, __ne__, __lt__, etc.) are all calling this custom _comparison function under the hood.

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)

As you can see, the code doesn't return a boolean result, it returns an instance of FilterNode which itself evaluates to a truthy/falsey value appropriately for the comparison.

How come the Account.userid >= 40 expression is not expanded at call time to true or false before passed as an argument?

It technically is getting expanded/evaluated before the query() function is called, it just isn't evaluating to a boolean value.