On Mon, 18 Oct 2021 at 11:20, Mathew Elman
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.
I have on occasion come across a situation where I use/write a signature like this:
def insert_x_into_y(x, y): ...
or worse
def insert_into(item, container): ...
where, despite a driving idea of python syntax being readability in english, the function signature is distinctly not english. "I'll just go and insert into this item that container", is not only never said but is actually ambiguous in english.
What would be really cool, is if python let you write function signatures like this:
def insert_(item)_into_(container): ...
where the arguments dispersed between the function name are positional only argument, and any key word arguments would have to go at the end.
If you care enough, you could create an API that looked like this: insert(1).into(my_list) The `insert` function would create an object that had an `into` method that did the actual work. Personally, I think that sort of API is taking things too far, and I wouldn't use it. Apart from anything else, it "steals" extremely common words like "insert" for your specific API. But if you want to do it, you can - without needing any change to Python.
It would create a function that could be called as:
insert_(1)_into_(my_list)
or
insert__into_(1, my_list)
The purpose of allowing both should be obvious - so that the function can be referenced and called in other places.
If you want `insert__into_` as well, just do def insert__into(x, y): return insert(x).into(y) But why would you? It's ugly if spelled like that, and your whole argument is that the "interspersed arguments" form is better. If you just want to pass the function to something that expects "normal" argument conventions, lambda x,y: insert(x).into(y) does what you want.
(Rather than just skipping the brackets the function call with only the end parentheses could have a special stand in character e.g. ., ?, !, _ or other if that was more preferred.)
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)
I've never heard anyone else suggest anything like this, so you might want to consider that the annoyance you feel is not a common reaction...
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.
Allowing each part to be called would make sense if each "next" partial function were an attribute on the previous and what it returned, making it a sort of object oriented currying. Then the syntax could be with a `.`:
def is_(obj)._an_instance_of_(type): ...
is_(1)._an_instance_of_(int) is_._an_instance_of_(1,int)
Yes, that's something like what I'm suggesting. Given that this can be done already in Python, I don't think there's anything like enough justification for special language support for it. Paul