Does anyone else use this little idiom?

Ben Finney bignose+hates-spam at benfinney.id.au
Sat Feb 2 23:08:34 EST 2008


miller.paul.w at gmail.com writes:

> 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."

Not to me. As you noted, '_' is easily ambiguous. Explicit is better
than implicit; the name 'dummy' makes it much clearer.

> 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.

The name '_' also has an established meaning for the "gettext"
internationalisation/localisation library, pre-dating Python and
documented in <URL:http://www.python.org/doc/lib/module-gettext>.

-- 
 \      "I have a large seashell collection, which I keep scattered on |
  `\            the beaches all over the world. Maybe you've seen it." |
_o__)                                                   —Steven Wright |
Ben Finney



More information about the Python-list mailing list