Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass !, !=, ~, <, > as parameters

I want to be able to pass arguments like this:

fn(a>=b) or fn(a!=b)

I saw this behavior in DjangoORM and SQLAlchemy but I don't know how to achieve it.

like image 508
user3485236 Avatar asked Oct 04 '15 13:10

user3485236


1 Answers

ORMs use special methods on classes for a and b to hook into operators and customise what is produced.

>= for is handled by the object.__ge__() method, while != calls object.__ne__().

Typically, the ORM object used for a returns a new object with the operation applied, allowing you to chain operations, and the fn() function expects such an ORM object and will read the operation status from there.

like image 132
Martijn Pieters Avatar answered Sep 23 '22 21:09

Martijn Pieters