[Tutor] Good Taste Question: Using SQLite3 in Python
Roel Schroeven
roel at roelschroeven.net
Wed Apr 29 23:11:13 CEST 2015
Alan Gauld schreef op 2015-04-29 18:43:
> On 29/04/15 17:03, Jugurtha Hadjar wrote:
>> http://docs.python-guide.org/en/latest/writing/style/#create-an-ignored-variable
>
> I've seen this before and strongly disagree with it.
I disagree with your disagreement. I'll try to explain.
> They are ambiguous, difficult to remember, easy to overlook
I'm not sure what exactly you mean by that.
> and if you change your mind and decide you need to use it later
> it's a whole new name to go back and introduce
True, but I don't see how that is a problem. At that point the variable
is only used at one point, so you only have to rename it at that one place.
> (or worse be tempted to use the meaningless symbol).
That would be bad indeed, but I think a very minimal amount of
discipline is enough to avoid that.
> And if you need another 'throw-away' name later you use the same one, then forget
> here are two uses and try to use it anyway (even in debugging) and get
> completely wrong values, that may or may not look different
> to what you expect.
The whole point of ignored variables is that you don't use them. If you
use them, they're not exactly ignored variables. It doesn't matter if
you use __ once or twice or many times more; all of them are to be ignored.
> (looking different is good - you can
> detect it easily, looking similar is very, very, bad!)...
> its just a horror story waiting to trip you up.
I'm not sure in what way __ can lead to horror stories. Do you have an
example to fuel my imagination?
> It's far better to have a short meaningful name that is never
> used than a bland, meaningless, generic symbol.
By 'short meaningful name', do you mean something like 'dummy' or
'ignorethis' as in
basename, dummy, ext = filename.rpartition('.')
or rather something like
basename, separator, ext = filename.rpartition('.')
In the first case, I prefer __ over dummy exactly because to me it's
clearer at a glance that the name is one-use only and the value is to be
ignored.
In the second case, using a 'real' name like 'separator' means I now
have to mentally keep track of it since as far as I can see at that
point it might be used later in the code.
To me, using _ or __ decreases the cognitive load because they tell me I
don't have to remember anything about them, since they're not going to
be used later. Read and forget.
Best regards,
Roel
--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov
Roel Schroeven
More information about the Tutor
mailing list