"Don't rebind built-in names*" - it confuses readers

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jun 10 22:56:43 EDT 2013


On Mon, 10 Jun 2013 20:14:55 -0400, Terry Jan Reedy wrote:

> For instance, open Lib/idlelib/GrepDialog.py in an editor that colorizes
> Python syntax, such as Idle's editor, jump down to the bottom and read
> up, and (until it is patched) find
>                      list.append(fn)
> with 'list' colored as a builtin. Stop. That looks wrong. List.append
> needs two arguments: a list instance and an object to append to the
> list. The 'solution' is in a previous line
>          list = []
> Reading further, one sees that the function works with two lists, a list
> of file names, unfortunately called 'list', and a list of
> subdirectories, more sensibly call 'subdirs'. 

Yes, that is a poor choice of names.

But sometimes you are dealing with a generic list, and calling it 
"filenames" would be equally inappropriate :-)


> I was initially confused
> and reading the code still takes a small bit of extra mental energy.
> Idle stays confused and will wrongly color the list instance name until
> it is changed. Calling the file list 'fnames' or 'filenames' would have
> been clearer to both me and Idle.

Correct. The downside of editors that colourise text is that sometimes 
they colourise it wrong. In this case, how is the editor supposed to know 
that list no longer refers to the built-in list?

This is yet another good argument for being cautious about shadowing 
built-ins.



-- 
Steven



More information about the Python-list mailing list