【发布时间】:2020-05-30 16:25:35
【问题描述】:
在调试复杂代码时,有时需要进行改造:
def myfunction(self):
...
self.foo.bar = self.baz.bla
进入
def myfunction(self):
...
self.foo.bar = self.baz.bla
print("myfunction", "self.foo.bar", self.foo.bar) # add this for debugging purposes
有没有办法(使用装饰器或上下文管理器或其他任何东西)自动print 变量名和下一行代码赋值的值(也可能是当前函数)?强>
例子:
def myfunction(self):
...
with debug:
self.foo.bar = self.baz.bla
会输出:
"myfunction self.foo.bar 123"
【问题讨论】:
标签: python debugging logging contextmanager