Does anyone else use this little idiom?

Thomas Dybdahl Ahle thomas at ahle.dk
Sun Feb 3 14:37:06 EST 2008


On Sat, 2008-02-02 at 18:03 -0800, miller.paul.w at gmail.com wrote:
> for _ in xrange (1,n):
>    some code

I'd always use i for loop variables I don't know what to call. It stands
for iterator or something. In a nested loop the next variable would
simply be called j and so on.

I also tend to use _, but in cases like:
_, _, something, _ = somecall()
where I only need one variable from a tupple. I only use this if I'm
sure the other values will probably never be of any use for the code.

However a large problem with _ is, that it is also the standard name of
the translate function of e.g. gettext.
In gui code you'll often find widget.setText(_("Save")). If you have
renamed _ this will give you problems.

> An alternative way of indicating that you don't care about the loop
> index would be
> 
> for dummy in xrange (1,n):
>    some code

I don't like dummy, as it is too long. I generelly spend more times
looking into long variable than short ones.

Regards, Thomas





More information about the Python-list mailing list