By passing a function to another function I meant passing a code block as an inline function to a function call.
The do statement is simply the arguments the function is called with
Brackets = optional as expr [do comma-separated-expressions]: block
means evaluate expr, then call the result of the expression. If do is present, call it with the argument list after do. Otherwise, call it with no arguments.
If you don't have practical uses, this proposal has ZERO chance of being accepted.
I have already shown a Flask use case and another poster has mentioned they would find it useful in their own code. (Though they had reservations on whether it should be included in Python.)
I simply said I wasn’t totally aware of all practical use cases, especially practical abuse cases.
This is an attempt to respond to the issues that be lying behind your request for code blocks in Python. It's my two cents worth (https://en.wikipedia.org/wiki/My_two_cents) of opinion.
I really do wish we could have language that had all of Ruby's strengths, and also all of Python's. That would be really nice. Quite something indeed.
But most strengths, in another situation, can be a weakness. Language design is often a compromise between conciseness and readability, ease of use and performance, good for beginners and good for experts, and many other factors. Such as innovation and stability.
Guido's decisions, as BDFL, have shaped Python and its community into what it is now. It is one set of compromises. Other languages, such as Ruby, have made different compromises. Now that the BDFL is on vacation, the challenge is to maintain the essence of Python while continuing to innovate.
Python's compromises and implementation give the language a large sweet spot. === https://en.wikipedia.org/wiki/Sweet_spot_(sports) The sweet spot is a place where a combination of factors results in a maximum response for a given amount of effort. ===
Languages do influence each other. Ruby is good at internal Domain Specific Languages (DSLs). And there's Perrotta's influential book on Ruby Metaprogramming. That's something I think Python could learn from.
But I don't see any need (or even benefit) in adding new language features to Python, so it can do better at DSLs. It's fairly easy to write a decorator that turns a class into a dictionary of 'code blocks'. I think that might be what you really want.
I'd be pleased to hear what you have to say about this.
with best regards
James Lu wrote:
as expr [do comma-separated-expressions]: block
means evaluate expr, then call the result of the expression. If do is present, call it with the argument list after do.
This kind of thing has been proposed before. Although it seems like a straightforward idea, there are some thorny edge cases concerning how things like 'return' and 'break' in the code block should be handled. For example, what should this do:
def f(): as g: return
Should that return from g or from f?
while some_codition: as g: if some_other_condition: break
If you want to flesh out your proposal you will have to address these. Also you should seek out previous discussions about code blocks to make sure you're not just rehashing old ground.