如何通过装饰器将函数中活着的变量名称打印出来? - Python 3

发布于 2025-02-12 01:46:05 字数 505 浏览 1 评论 0原文

因此,我有这个代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2025-02-19 01:46:05

装饰者无法在您的线之间提出命令。您的变量仅在功能的运行时存在,因此无法在其他任何地方读取它们。您必须将它们定义为类变量或 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文