You cannot redefine methods on built-in types, and you cannot change the default value of the errors
parameter to str.decode()
. There are other ways to achieve the desired behaviour, though.
The slightly nicer way: Define your own decode()
function:
def decode(s, encoding="ascii", errors="ignore"):
return s.decode(encoding=encoding, errors=errors)
Now, you will need to call decode(s)
instead of s.decode()
, but that's not too bad, isn't it?
The hack: You can't change the default value of the errors
parameter, but you can overwrite what the handler for the default errors="strict"
does:
import codecs
def strict_handler(exception):
return u"", exception.end
codecs.register_error("strict", strict_handler)
This will essentially change the behaviour of errors="strict"
to the standard "ignore"
behaviour. Note that this will be a global change, affecting all modules you import.
I recommend neither of these two ways. The real solution is to get your encodings right. (I'm well aware that this isn't always possible.)