Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop myself overwriting Python functions when coding?

A source of constant headache when tracking down bugs in my Python code are seemingly innocuous snippets like this:

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

This fails because I've overwritten the function list() with the variable list.

A contrived example obviously, but the point is Python happily lets me overwrite built-in functions with variables. I realise this is a crucial feature in Python but I would quite like it if the interpreter would warn me when I do it in my code as I usually don't mean to do this.

Can anyone suggest a solution (other than just being more careful) - as I keep tripping over this problem?

like image 1000
Nick Loman Avatar asked Mar 02 '11 14:03

Nick Loman


People also ask

How do you stop overwrite in Python?

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position).

How do you stop overwrite names in Python?

You can't prevent it, Python is built on the notion of "we're all consenting adults" so you are free to do what you want. It is possible to delete the override but you'd have to know that you shadowed that function in the first place.

How to override a built in method in Python?

In Python method overriding occurs by simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.


1 Answers

You should use Pylint. If you are using Eclipse + PyDev, you can configure it to run automatically within the IDE and highlight this issue (and many many others).

like image 79
Botond Béres Avatar answered Oct 11 '22 12:10

Botond Béres