I am learning sqlachemy, I'm relatively new to Python.
When I read its documentation, I saw this kind of usage, for example:
query.filter(User.name == 'ed')
Wouldn't Python evaluate the expression User.name == 'ed'
and then pass the result, which is a boolean, to query.filter
method?
How does this kind of syntax work? Does Python support some kind of operator overriding like C++?
SQLAlchemy uses the various special method hooks to overload operator behaviour.
For
==
, the__eq__()
method returns special objects that signify a SQL expression when compiled. To quote the documentation on the 'rich comparison' hooks:See the
ColumnOperators
class in the SQLAlchemy source for the specific hooks implemented.