3

Guido van Rossum's Ndb library for Google App Engine has a syntax for queries that I find curious. Suppose you wanted to find all happy users, you would write this query:

User.query(User.happy == True)

I read that as:

  • A function call of the query method of the User class, which inherits from ndb.Model. Fine so far.
  • The one argument of the function call is an expression doing a boolean comparison between a class property, User.happy, and a value, True.

I would expect the expression to evaluate to False (much as dict.keys == True would), so the above should be equivalent to:

User.query(False)

How does Ndb do anything intelligent with that? What python magic am I missing?

1 Answer 1

8

You're missing python magic methods.

__eq__(self, other)

Defines behavior for the equality operator, ==.

Good guide about this could be found here.

2
  • Ok, that's clever. Thanks! I still find it odd-looking. In particular, it seems to run afoul of PEP 8, "Don't compare boolean values to True or False using ==". I understand that you're saying this isn't really a boolean comparison. Still, my brain's syntax checker hiccups every time.
    – Chris
    Mar 26, 2015 at 15:26
  • Yes, I had to turn E711 & E712 off due to this syntax. Mar 27, 2015 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.