[Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
Hi Pythonistas, yet again today I ended up writing: d = [[0] * 5 for _ in range(10)] And wondered, why don't we have a way to repeat other than looping over range() and using a dummy variable? This seems like a rather common thing to do, and while the benefit doesn't seem much, something like th

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Nick Coghlan
On 30 March 2017 at 19:18, Markus Meskanen wrote: > Hi Pythonistas, > > yet again today I ended up writing: > > d = [[0] * 5 for _ in range(10)] > > And wondered, why don't we have a way to repeat other than looping over > range() and using a dummy variable? Because it's relatively rare to not us

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
On Mar 30, 2017 12:53, "Nick Coghlan" wrote: Because it's relatively rare to not use the loop variable for anything (even if it's just a debug message), and in the cases where you genuinely don't use it, a standard idiom can be applied (using a single or double underscore as a dummy variable), ra

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
On Thu, Mar 30, 2017 at 12:53 PM, Nick Coghlan wrote: > > Because it's relatively rare to not use the loop variable for anything > (even if it's just a debug message), and in the cases where you > genuinely don't use it, a standard idiom can be applied (using a > single or double underscore as a d

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Allan Clark
If there were to be special syntax for this case, I'd just allow an empty pattern, such as: d = [[0] * 5 for in 10] This is exactly the same as your 'repeat_for' except that it is spelt 'for in', which means there are no new keywords. It would also be allowed in for-loops in the same way as y

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Allan Clark
Sorry, that obviously should have been: d = [[0] * 5 for in range(10)] So it's not quite exactly the same as your example. On 30 March 2017 at 11:24, Allan Clark wrote: > If there were to be special syntax for this case, I'd just allow an > empty pattern, such as: > > d = [[0] * 5 for in

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Mark E. Haase
Your example is really repeating two things: d = [ [0 for _ in range(5)] for _ in range(10) ] But since list() uses * for repetition, you could write it more concisely as: d = [[0] * 5] * 10] I'm not picking on your specific example. I am only pointing out that Python gives you the tools you ne

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Daniel Moisset
That's not the same, in the OP example you can assert d[0] is not d[1], while in your code that assertion fails (the list comprehension evaluates the expression each time creating a new list, your code makes 10 references to a single 5 element list) On 30 March 2017 at 14:51, Mark E. Haase wrote

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Brice PARENT
Le 30/03/17 à 15:51, Mark E. Haase a écrit : I'm not picking on your specific example. I am only pointing out that Python gives you the tools you need to build nice APIs. If repetition is an important part of something you're working on, then consider using itertools.repeat, writing your own d

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Pavol Lisy
On 3/30/17, Nick Coghlan wrote: > On 30 March 2017 at 19:18, Markus Meskanen > wrote: >> Hi Pythonistas, >> >> yet again today I ended up writing: >> >> d = [[0] * 5 for _ in range(10)] d = [[0]*5]*10 # what about this? >> And wondered, why don't we have a way to repeat other than looping

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Nick Coghlan
On 31 March 2017 at 00:23, Pavol Lisy wrote: > Just for curiosity - if PEP-501 will be accepted then how many times > could be fnc called in next code? > > eval(i'{fnc()}, ' *3) Once (the same as f-strings), but then it would throw TypeError, as unlike strings and other sequences, Interpolati

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Kyle Lahnakoski
On 2017-03-30 05:18, Markus Meskanen wrote: > Hi Pythonistas, > > yet again today I ended up writing: > > d = [[0] * 5 for _ in range(10)] > > > Thoughts? It looks like you are initializing matrices. Can you make a helper function? d = matrix(shape=(5, 10), default=0) or maybe use NumPy?

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Wolfgang Maier
On 03/30/2017 04:23 PM, Pavol Lisy wrote: On 3/30/17, Nick Coghlan wrote: On 30 March 2017 at 19:18, Markus Meskanen wrote: d = [[0] * 5 for _ in range(10)] d = [[0]*5]*10 # what about this? These are not quite the same when the repeated object is mutable. Compare: >>> matrix1 = [

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
Just an example among many :) On Mar 30, 2017 6:07 PM, "Kyle Lahnakoski" wrote: > > > On 2017-03-30 05:18, Markus Meskanen wrote: > > Hi Pythonistas, > > > > yet again today I ended up writing: > > > > d = [[0] * 5 for _ in range(10)] > > > > > > Thoughts? > > It looks like you are initializing

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Joao S. O. Bueno
On 30 March 2017 at 10:51, Mark E. Haase wrote: > Your example is really repeating two things: > > d = [ [0 for _ in range(5)] for _ in range(10) ] > > But since list() uses * for repetition, you could write it more concisely > as: > > d = [[0] * 5] * 10] > > I'm not picking on your specific examp

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
On Mar 30, 2017 19:04, "Joao S. O. Bueno" wrote: On 30 March 2017 at 10:51, Mark E. Haase wrote: > Your example is really repeating two things: > > d = [ [0 for _ in range(5)] for _ in range(10) ] > > But since list() uses * for repetition, you could write it more concisely > as: > > d = [[0] *

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Joao S. O. Bueno
On 30 March 2017 at 13:10, Markus Meskanen wrote: > > > On Mar 30, 2017 19:04, "Joao S. O. Bueno" wrote: > > On 30 March 2017 at 10:51, Mark E. Haase wrote: >> Your example is really repeating two things: >> >> d = [ [0 for _ in range(5)] for _ in range(10) ] >> >> But since list() uses * for re

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
And like I said before, for loop is just another way of doing while loop, yet nobody's complaining. There's nothing wrong with having two different ways of doing the same thing, as long as one of them is never the better way. If we add `repeat`, there's never a reason to use `for _ in range` anymor

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Brice PARENT
Le 30/03/17 à 19:06, Markus Meskanen a écrit : And like I said before, for loop is just another way of doing while loop, yet nobody's complaining. There's nothing wrong with having two different ways of doing the same thing, as long as one of them is never the better way. If we add `repeat`, t

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Markus Meskanen
On Thu, Mar 30, 2017 at 8:23 PM, Brice PARENT wrote: > > Le 30/03/17 à 19:06, Markus Meskanen a écrit : > >> And like I said before, for loop is just another way of doing while loop, >> yet nobody's complaining. There's nothing wrong with having two different >> ways of doing the same thing, as l

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Rhodri James
On 30/03/17 18:06, Markus Meskanen wrote: And like I said before, for loop is just another way of doing while loop, yet nobody's complaining. There's nothing wrong with having two different ways of doing the same thing, as long as one of them is never the better way. If we add `repeat`, there's n

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Pavol Lisy
On 3/30/17, Nick Coghlan wrote: > On 31 March 2017 at 00:23, Pavol Lisy wrote: >> Just for curiosity - if PEP-501 will be accepted then how many times >> could be fnc called in next code? >> >> eval(i'{fnc()}, ' *3) > > Once (the same as f-strings), but then it would throw TypeError, as > unl

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Joshua Morton
A for loop in python saves an enormous amount of boilerplate code though (I would post an example, but I'd likely mess up a while loop over an iterator from memory if I posted it here). The `for x in y` construct saves multiple lines an an enormous amount of boilerplate and mental strain in the maj

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Pavol Lisy
On 3/30/17, Joshua Morton wrote: >def repeat_for(func, iters): > return func() for _ in range(iters) > > does what you want without any required syntax changes. I've got a "SyntaxError: invalid syntax" PL. ___ Python-ideas mailing list Pyt

Re: [Python-ideas] What about regexp string litterals : re".*" ?

2017-03-30 Thread Abe Dillon
> a huge advantage of REs is that they are common to many > languages. You can take a regex from grep to Perl to your editor to > Python. They're not absolutely identical, of course, but the basics > are all the same. Creating a new search language means everyone has to > learn anew. > ChrisA 1)

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Nick Coghlan
On 31 March 2017 at 04:08, Pavol Lisy wrote: > On 3/30/17, Nick Coghlan wrote: >> On 31 March 2017 at 00:23, Pavol Lisy wrote: >>> Just for curiosity - if PEP-501 will be accepted then how many times >>> could be fnc called in next code? >>> >>> eval(i'{fnc()}, ' *3) >> >> Once (the same as

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Steven D'Aprano
On Thu, Mar 30, 2017 at 04:23:05PM +0200, Pavol Lisy wrote: > On 3/30/17, Nick Coghlan wrote: > > On 30 March 2017 at 19:18, Markus Meskanen > > wrote: > >> Hi Pythonistas, > >> > >> yet again today I ended up writing: > >> > >> d = [[0] * 5 for _ in range(10)] > > d = [[0]*5]*10 # what abo

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Nick Coghlan
On 31 March 2017 at 03:06, Markus Meskanen wrote: > And like I said before, for loop is just another way of doing while loop, > yet nobody's complaining. There's nothing wrong with having two different > ways of doing the same thing, as long as one of them is never the better > way. If we add `rep

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Nick Coghlan
On 31 March 2017 at 15:12, Nick Coghlan wrote: > So *if* we were to add anything to the language here, it would be to > add `itertools.repeat_call` as a new iteration primitive, since it > isn't entirely straightforward to construct that operation out of the > existing primitives, with itertools.s

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Steven D'Aprano
On Thu, Mar 30, 2017 at 12:59:57PM +0300, Markus Meskanen wrote: > And instead of learning a special syntax, which is simple and easy to > understand when they google "repeat many times python", they now end up > learning a special semantic by naming the variable with an underscore. Let me prefac

Re: [Python-ideas] Way to repeat other than "for _ in range(x)"

2017-03-30 Thread Suresh V. via Python-ideas
On Thursday 30 March 2017 02:48 PM, Markus Meskanen wrote: Hi Pythonistas, yet again today I ended up writing: d = [[0] * 5 for _ in range(10)] And wondered, why don't we have a way to repeat other than looping over range() and using a dummy variable? This seems like a rather common thing to d