
On Mon, Oct 18, 2021 at 9:22 PM Mathew Elman <mathew.elman@ocado.com> wrote:
I don't know if this has been suggested before, or if this is outlandishly impossible (though I would be surprised if it was), so apologies in advance if so.
As stated, it basically is.... but the cool thing about crazy ideas is, there's often a very similar one that CAN be implemented! What you're basically asking for is a special type of operator. Python ships with operators like "*" meaning multiplication, where you write the operator between the two values. You can create your own using some very simple abuses of syntax and a nice library.
This sort of signature is particularly annoying for boolean checks like `isinstance` (N.B. I am _not_ suggesting changing any builtins), which one could wrap with:
def is_(obj)_an_instance_of_(type): return isinstance(obj, type)
For precedence in other languages, this is similar to curried functions in functional languages e.g Haskell, especially if each part of a function were to be callable, which would be up for debate.
How about this: https://pypi.org/project/infix/ @div_infix def instance_of(obj, type): return isinstance(obj, type)
42 /instance_of/ int True
It's not precisely what you were asking about, but it works on all current versions of Python, and allows a lot of flexibility. ChrisA