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?
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).
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With