Re: [Python-ideas] Syntactic Sugar: Post-Conditions for Guard Clauses

2018-05-17 Thread Chris Barker via Python-ideas
On Thu, May 17, 2018 at 12:55 PM, Manuel Barkhau  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.bar...@noaa.gov
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Syntactic Sugar: Post-Conditions for Guard Clauses

2018-05-17 Thread Manuel Barkhau
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
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/