Does anyone else use this little idiom?
miller.paul.w at gmail.com
miller.paul.w at gmail.com
Sun Feb 3 12:43:55 EST 2008
My apologies if any attributions are messed up.
On Feb 3, 1:28 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 03 Feb 2008 15:08:34 +1100, Ben Finney wrote:
> >> 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.
Actually, "_" isn't ambiguous at all. It's a perfectly well-defined
variable name. It just seems most people don't know that, probably
because most people never get the urge to name a variable "_".
The whole reason I find myself wanting to use "_" is that "for _ in
xrange (n):" goes beyond explicit to redundant if you're not using the
index variable inside the loop. Ruby's version is much better in this
respect, because every token matters.
> In
> fact, to me "dummy" implies that it holds a dummy value that will be
> replaced later with the actual value needed.
>
> If you want an explicit name, try a variation of "dontcare". Assuming
> that you're an English speaker.
That reminds me... I saw some code once that used a dummy variable
named "dont_give_a_shit". I got a few seconds of giggles out of it,
at least. :-)
> People seem to forget that "explicit" just means that there's a
> convention that nearly everybody knows, and if you follow it, nearly
> everybody will know what you mean. Often that convention is nothing more
> than the meanings of words in whatever human language you're speaking,
> but it's still a convention.
Well, I'd argue that's only part of what "explicitness" means. In
this specific use case, having to write the index variable isn't being
explicit; it's a simple redundancy. Writing "_", or "dontcare" or
"dont_give_a_shit" doesn't seem more explicit at all. It seems silly
to have to write it at all if I don't intend to use it. I'd really
prefer something like the Ruby syntax because all you have to write is
the number of times you want to do something, and then the thing you
want to do. (I guess, in that respect, I'd even consider the xrange
call a redundancy.)
Thanks, everyone for the replies. :-)
More information about the Python-list
mailing list