[Python-ideas] if in for-loop statement

2017-02-23 Thread Henk-Jaap Wagenaar
Hi all, Often I have typed something like for x in range(100) if is_prime(x): # do things with x to find that this does not work, instead resorting to: for x in range(100): if is_prime(x): # do things with x or for x in range(100): if not is_pri

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Paul Moore
On 23 February 2017 at 13:37, Henk-Jaap Wagenaar wrote: > Note that this has been suggested before at least once > (https://mail.python.org/pipermail/python-dev/2007-November/075257.html), > and that thread itself suggests it has been suggested before and shutdown by > Guido (though no source is g

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Daniel Moisset
Just as a reference, I think the most recent reincarnation of this thread was: https://mail.python.org/pipermail/python-ideas/2016-September/042270.html On 23 February 2017 at 13:46, Paul Moore wrote: > On 23 February 2017 at 13:37, Henk-Jaap Wagenaar > wrote: > > Note that this has been sugges

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Henk-Jaap Wagenaar
Hi Paul, Daniel, others, That is fair enough and the right amount of blunt. Let me go through the points that were made in that thread (I have not found any other threads, if someone links to them, I will go through them as well): - From: https://mail.python.org/pipermail/python-dev/2007-No

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Jelle Zijlstra
2017-02-23 5:37 GMT-08:00 Henk-Jaap Wagenaar : > > Hi all, > > Often I have typed something like > > for x in range(100) if is_prime(x): > # do things with x > > to find that this does not work, instead resorting to: > > for x in range(100): > if is_prime(x): > #

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Paul Moore
On 23 February 2017 at 14:20, Henk-Jaap Wagenaar wrote: > > In a straw poll at the company I work at everyone was in favour, though they > obviously are not in charge of implementing or changing documentation so > that is easy for them to say, they've got no skin in the game. I don't know > whethe

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Henk-Jaap Wagenaar
One does not seem to be able to do this in a generator expression: foo = (x for x in [1, 2] if True else [1, 2, 3]) gives a syntax error, however, adding parenthesis 'solves' this: foo = (x for x in [1, 2] if True else [1, 2, 3]) In the for-loop version, either works. Though I guess this would

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Henk-Jaap Wagenaar
Hi Paul, Thanks for typing that all out and taking the time to respond to my emails. I think as you say, it might be good to put this somewhere obvious. I did find https://docs.python.org/devguide/langchanges.html and http://www.curiousefficiency.org/posts/2011/02/justifying-python-language-chang

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Paul Moore
On 23 February 2017 at 15:18, Henk-Jaap Wagenaar wrote: > Thanks for typing that all out and taking the time to respond to my emails. > I think as you say, it might be good to put this somewhere obvious. > > I did find https://docs.python.org/devguide/langchanges.html and > http://www.curiouseffic

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Sven R. Kunze
Hi Henk-Jaap, thanks for your "if in for" proposal. Paul's comments are all "motherhood statements" against a generic proposal. It's nothing that would prevent your specific proposal from being accepted or not. And there's no rejected PEP for this feature as far as I can see. Skimming throu

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Steven D'Aprano
On Thu, Feb 23, 2017 at 03:02:18PM +, Paul Moore wrote: > We really need a FAQ for this, if there isn't one already. Indeed. Earlier this year, I started a thread on Things That Won't Change In Python. If I recall correctly, Nick disagreed that it should be a PEP, I started to write up a

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Steven D'Aprano
On Thu, Feb 23, 2017 at 03:33:32PM +, Paul Moore wrote: > Ha. I never even *thought* of putting something like this in the > devguide. Thanks for the pointer. I'll try to make some time to polish > up my comments and put a PR together for that. Paul, at the moment I have neither the time nor

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Steven D'Aprano
On Thu, Feb 23, 2017 at 01:37:15PM +, Henk-Jaap Wagenaar wrote: [...] > Other solutions to another case of this 'problem' are discussed has been > discussed on StackOverflow ( > http://stackoverflow.com/questions/6981717/pythonic-way-to-combine-for-loop-and-if-statement) > where it is suggeste

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Régis Martin
Hello, why not using the filter function? for x in filter(is_prime, range(100)): # do something with x This is equivalent as was mentionned at first: for x in range(100) if is_prime(x): # do things with x Régis -- Régis Martin Directeur de Projets Astek Industrie Tel: 06-83-5

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Henk-Jaap Wagenaar
I think, in that particular example, that is an ok solution, however if you are combining multiple booleans to create a lambda or separate function that is quite a bit of syntactical sugar to add (unless it is re-used). H-J On 23 February 2017 at 18:11, Régis Martin wrote: > Hello, > why not us

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Sven R. Kunze
Hey Steven, On 23.02.2017 19:25, Steven D'Aprano wrote: Indeed not. The Pythonic solution is exactly the one you used: DON'T combine the for-loop and if-statement. for x in range(100): if is_prime(x): ... If the use of two lines and two indents is truly a problem, to the point th

Re: [Python-ideas] math.nextafter

2017-02-23 Thread Chris Barker
On Mon, Feb 6, 2017 at 5:42 AM, Juraj Sukop wrote: > Do you mean something like: > > isclose(f(x), 0.0, rel_tol, abs_tol) > > If so, what should `rel_tol` and `abs_tol` be? > isclose is mostly about "relative" closeness, so rel_tol is more-or-less the number of decimal digits you want the s

Re: [Python-ideas] Fwd: Define a method or function attribute outside of a class with the dot operator

2017-02-23 Thread Chris Barker
Has this REALLY not been discussed and rejected long ago? > But I'm +1 on writing a PEP -- collect all these pros and cons in one > place to save on future discussion. (And (good) PEP writing is a way to > earn valuable Python Points!) > Exactly -- this is obvious enough that it WILL come u

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Steven D'Aprano
On Thu, Feb 23, 2017 at 11:07:49PM +0100, Sven R. Kunze wrote: > >If the use of two lines and two indents is truly a problem, to the point > >that you really need to refactor to a single line, that's a code smell: > >your function is probably too big, too complex and does too much, and > >should b

Re: [Python-ideas] if in for-loop statement

2017-02-23 Thread Chris Angelico
On Fri, Feb 24, 2017 at 3:28 PM, Steven D'Aprano wrote: > There is nothing wrong with a nested for...if pair of statements. But > that does take two lines, and two indents, rather than one: > > block > for ... > if ... > block > > versus hypothetical: > > block >