I have encountered it in other languages - what they have in common is brace-delimited or similar syntax rather than whitespace-significant syntax. I’ve often found the “idiomatic” way of doing things cumbersome in those languages (multiple consecutive occurrences of )};
in JavaScript code, or permutations thereof, are not my idea of “fun to read”) and I feel that it would be much worse in Python.
I have encountered many developers (and students) who I felt needlessly broke lines of code up into smaller statements and “baby-stepped” through the code. But sometimes names are useful, and sometimes breaking up lines is useful. Not all languages can be Lisp. If a function isn’t itself just making some trivial adaption to an existing function (such as one can easily do with lambda
or functools.partial
, in a way that the name of the wrapped function is still prominent), then it’s doing something according to its own logic; and that task probably deserves a name.
Let’s consider what it looks like when we pass a complex callback function, something where we want to write multiple statements in order to implement the logic. Currently we do something like:
def callback(framework_arg):
...
...
api_call(1, 2, callback, kw="example")
If we want to “inline” the callback with any kind of multiple-line, whitespace-significant syntax - then yes, we get to avoid naming the callback and then using that name (and to be fair, names are hard; “callback” doesn’t mean much in this example, and in real-world code a better name might not make itself obvious). But now we have to completely rethink the formatting of all the other arguments for the call. Does our new syntax let us start on the same line, like api_call(1, 2, magic_keyword
etc., the way that a lambda
does already? If so, how are lines inside the callback function indented relative to api_call
? Or must their indentation be relative to the magic_keyword
? How does that interact with existing guidelines and conventions for wrapping the other arguments for a multi-line (parenthesized) call? What about the argument afterward - is it forced onto a separate line? What about the comma after the callback - can it appear “inside” the function block, given some delimiter?
Yes, you can try to answer these questions; but so far, the consensus is that none of the possible answers are any good. Even if you find an aesthetic compromise that you can get everyone to be satisfied with, I don’t think you’ll find that it actually saves vertical space, anyway. And then it deprives you of a compiler-recognized way to explain what the callback actually does (i.e., its name).