Python 行分析时注释掉@profile decorators
Python行评测的流行解决方案似乎是Python 行分析时注释掉@profile decorators,python,profiling,Python,Profiling,Python行评测的流行解决方案似乎是kernprof-lscript.py,这需要在要评测的函数中添加“@profile”修饰符。在没有aspython script.py的情况下运行相同的代码会抱怨“name'profile'未定义”,因此您必须注释掉@profile行。在“配置文件”和非配置文件模式之间切换而不必注释掉这些行,有什么好办法呢?您可以尝试在脚本顶部添加类似的内容: try: profile # throws an exception when profile isn
kernprof-lscript.py
,这需要在要评测的函数中添加“@profile”修饰符。在没有aspython script.py的情况下运行相同的代码会抱怨“name'profile'未定义”,因此您必须注释掉@profile
行。在“配置文件”和非配置文件模式之间切换而不必注释掉这些行,有什么好办法呢?您可以尝试在脚本顶部添加类似的内容:
try:
profile # throws an exception when profile isn't defined
except NameError:
profile = lambda x: x # if it's not defined simply ignore the decorator.
这样,如果未定义,profile
函数,则可以将其定义为无操作装饰器