问题
Is it possible in Python to trace and filter functions that are called on strings during program run? I want to add sys.setdefaultencoding("utf-8")
application, and I want to set some guards to predict potential problems with misusing standard functions (like len
, for example), to process such strings.
回答1:
You can replace the builtin:
import __builtin__
real_len = __builtin__.len
def checked_len(s):
... do extra checks ...
return real_len(s)
__builtin__.len = checked_len
来源:https://stackoverflow.com/questions/29586776/trace-functions-that-are-called-on-python-strings