How to stop rewriting Python functions when coding?

The source of the constant headache when tracking errors in my Python code is seemingly harmless snippets like this:

list = ['a', 'b', 'c', 'c'] list(set(list)) 

This fails because I rewrote the list of functions () using a list of variables.

A contrived example, obviously, but the Python dot happily allows me to overwrite inline functions with variables. I understand that this is a very important function in Python, but I would really like the interpreter to warn me when I do this in my code, because I usually do not want to do this.

Can someone suggest a solution (other than being more careful) - how do I keep stumbling about this problem?

+6
function python builtin
source share
5 answers

Use a text editor that emphasizes syntax that selects keywords in a different color from the rest of the code.

+3
source share

You must use Pylint . If you use Eclipse + PyDev, you can configure it to run automatically in the IDE and highlight this problem (and many others).

+9
source share

Tools like PyChecker can be valuable to you. See also this discussion of SO.

+3
source share

Accidentally, using reserved names is a common problem; a common remedy is to use "good" names for your own objects (in a broad sense). โ€œGoodโ€ here means names that tell you relevant facts about a named object based on the context of the problem to be solved.

For toy problems, this may seem like a simple effort, but why not train a good name, even when you just write code to learn (features) of the language? So use your version

 list_with_duplicates = [ ... ] 
+2
source share

pylint will find this error (among many others).

0
source share

All Articles