如何通过装饰器将函数中活着的变量名称打印出来? - Python 3
因此,我有这个代码:
def checkScope(f):
def inner(*args):
res=f(*args)
// I WANNA PRINT HERE BIM BUM AND BAM
return res
return inner
class A():
@checkScope
def first(self):
bim = 5
return self.second(bim)
def second(self,m):
if m < 8:
bum = 6
return self.third()
def third(self):
bam=2
return bam
test=A()
test.first()
如何使用Inspect在功能CheckScope,变量BIM BUM和BAM中打印?
So, i've this code:
def checkScope(f):
def inner(*args):
res=f(*args)
// I WANNA PRINT HERE BIM BUM AND BAM
return res
return inner
class A():
@checkScope
def first(self):
bim = 5
return self.second(bim)
def second(self,m):
if m < 8:
bum = 6
return self.third()
def third(self):
bam=2
return bam
test=A()
test.first()
How can i print, inside the function checkScope, the variables bim bum and bam, using Inspect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
装饰者无法在您的线之间提出命令。您的变量仅在功能的运行时存在,因此无法在其他任何地方读取它们。您必须将它们定义为类变量或 globals 。
您可以做的一件事是将它们全部作为参数传递并阅读。
Decorators can't put commands between your lines. Your variables only exist for the runtime of the function, so they cannot be read anywhere else. You'd have to define them as class variables or globals.
The one thing you could do is to pass them all as parameters and read those.