python - Modify/Change __add__ magic method for str type -
i want attach/concatenate string bytes of python3 solving typeerror exception. use sample code:
from forbiddenfruit import curse def __add__(self, a): """ test """ if isinstance(a, bytes): self += a.decode('utf-8') else: self += curse(str, '__add__', __add__) s = "sample string"a print(s + "encode string".encode('utf-8'))
and returns error below (exactly normal state):
traceback (most recent call last): file "test1.py", line 16, in <module> print(s + "encode string".encode('utf-8')) typeerror: can't convert 'bytes' object str implicitly
i want stop theses exceptions , instead transparently decodes bite arrays if encoded "utf-8". can't changed line of these exceptions(bc there're tons of codes).
i want find way change source code of python ignore these case. indeed want extend/edit python core (str type).
how can this? or how can control exceptions @ runtime before raising up? (such monkey patch)
i think there's must done c extending api overriding built ins functions , type.
Comments
Post a Comment