Syntactic Sugar: Post-Conditions for Guard Clauses
![](https://secure.gravatar.com/avatar/0b5dac44355108c8efc18784e8f732a4.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
On Thu, May 17, 2018 at 12:55 PM, Manuel Barkhau <mbarkhau@gmail.com> wrote:
continue if not is_valid(elem) ...
how is this better than: if not is_valid(elem): continue ? But even if it is, that might be a consideration for a new language, but adding it to python now is pretty darn unlikely.
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.
now I"m confused -- if it's not a great sytle, why suggest it? and I was thinking that the one good thing is that the "return" or "break" is a bit more prominent -- which could be a good thing. Alternatively, is there a good way I can implement this as a
preprocessor for myself?
I'm pretty sure not -- python is very dynamic, but not let you redefine the language semantics. Other than as a import hook that re-wrote the code on the fly. but really -- don't do that -- then no one else will understand your code. -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (2)
-
Chris Barker
-
Manuel Barkhau