[Python-ideas] Syntactic Sugar: Post-Conditions for Guard Clauses
Manuel Barkhau
mbarkhau at gmail.com
Thu May 17 15:55:08 EDT 2018
Hello,
has there been consideration for implementing the following new
syntax:
def my_fun(elements):
results = []
for elem in elements:
...
continue if not is_valid(elem)
...
results.append(result)
break if len(results) == max_results
return results
Or similarly:
def iter_fun(elements):
num_results = 0
for elem in elements:
...
continue if not is_valid(elem)
...
yield result
num_results += 1
return if num_results == max_results
When there is an expression involved for the case of a `return`
or `raise` statement, I don't think it's such a great style,
because the conditional gets hidden off to the right.
def myfun(val):
return default_result if val not in known_vals
...
return result
Alternatively, is there a good way I can implement this as a
preprocessor for myself?
Thanks
Manuel Barkhau
More information about the Python-ideas
mailing list