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

Brice PARENT contact at brice.xyz
Thu Mar 30 10:10:57 EDT 2017


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 domain-specific repeat() 
> method, or even override * like list() does. One of the coolest 
> aspects of Python is how a relatively small set of abstractions can be 
> combined to create lots of useful behaviors.
>
> For students, the lack of a "repeat" block might be confusing at 
> first, but once the student understands for loops in general, it's an 
> easy mental jump from "using the loop variable in the body" to "not 
> using the loop variable in the body" to "underscore is the convention 
> for an unused loop variable". In the long run, having one syntax that 
> does many things is simpler than having many syntaxes that each do one 
> little thing.
+1
I would add that it is even the convention for all unused variables, not 
only in loops, as it is also used in other cases, like this for example :
key, _, value = "foo:date:bar".split(":")


More information about the Python-ideas mailing list