Does anyone else use this little idiom?
Stef Mientki
stef.mientki at gmail.com
Sun Feb 3 08:25:15 EST 2008
be careful, "_" is thé translation function used in Il8N, Il10N
localization / internationalization
e.g.
print _( "hello" )
cheers,
Stef
miller.paul.w at gmail.com wrote:
> Ruby has a neat little convenience when writing loops where you don't
> care about the loop index: you just do n.times do { ... some
> code ... } where n is an integer representing how many times you want
> to execute "some code."
>
> In Python, the direct translation of this is a for loop. When the
> index doesn't matter to me, I tend to write it as:
>
> for _ in xrange (1,n):
> some code
>
> An alternative way of indicating that you don't care about the loop
> index would be
>
> for dummy in xrange (1,n):
> some code
>
> But I like using _ because it's only 1 character and communicates well
> the idea "I don't care about this variable."
>
> The only potential disadvantages I can see are threefold:
>
> 1. It might be a little jarring to people not used to it. I do admit
> it looks pretty strange at first.
>
> 2. The variable _ has special meaning at the interactive interpreter
> prompt. There may be some confusion because of this.
>
> 5. Five is right out. (ob Holy Grail reference, of course. :-)
>
> So, I guess I'm wondering if anyone else uses a similar idiom and if
> there are any downsides to it that I'm not aware of.
>
> Thanks
>
> Paul
>
More information about the Python-list
mailing list