[Tutor] Good name(s) for to-be-ignored variables, was Re: Good Taste Question: Using SQLite3 in Python

Peter Otten __peter__ at web.de
Thu Apr 30 00:32:57 CEST 2015


Roel Schroeven wrote:

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

Inside a function there's a middle ground, a leading underscore:

def whatever():
    ...
    basename, _extsep, ext = filename.rpartition(os.extsep)
    ...

The name makes it clear what _extsep is supposed to contain, and the 
underscore indicates that you are not using the name.

You can later question the decision that you are not interested in _extsep 
-- in the example you may decide that you want to discriminate between

"somefile." and "somefile"

Even if you stick with the original design I find it more readable to state 
explicitly what you are throwing away. 

My whatever() function is an example where the name of the to-be-ignored 
variable led to a change in the code: I started with _separator and then 
remembered that the separator need not be a dot.

Admittedly I don't expect to use an OS where os.extsep != "." anytime soon, 
but you get the idea...



More information about the Tutor mailing list