7

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 3
    It's not quite what you're asking for, but in the general case you can pass e.g. `operator.ne` (for `!=`) and `operator.gt` (for `>`) around – jonrsharpe Oct 04 '15 at 13:41

1 Answers1

8

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343