在 Python 字符串上调用的跟踪函数 [英] Trace functions that are called on Python strings

查看:36
本文介绍了在 Python 字符串上调用的跟踪函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Python 中跟踪和过滤程序运行期间对字符串调用的函数?我想添加 sys.setdefaultencoding("utf-8") 应用程序,并且我想设置一些保护来预测误用标准函数(例如 len,对于例如),处理这样的字符串.

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.

推荐答案

可以替换内置的:

import __builtin__

real_len = __builtin__.len

def checked_len(s):
    ... do extra checks ...
    return real_len(s)

__builtin__.len = checked_len

这篇关于在 Python 字符串上调用的跟踪函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆