首页 > 解决方案 > 从被调用函数访问类属性?

问题描述

假设我有这样的事情:

  --module1

  def called():
     if caller.class.attrX == 1 : ...

  --module2
  class ABC:
     attrX = 1
     def method():
        called()

我想访问调用者 Class-attribute 吗?

我知道我必须以某种方式使用检查,但可以弄清楚到底如何。蟒蛇3

标签: python-3.xclassattributesinspect

解决方案


将变量传递给函数是最好的(也是唯一的?)选项。

 --module1

  def called(attrX):
     if attrX == 1 : ...

  --module2
  class ABC:
     self.attrX = 1
     def method():
        called(self.attrX)

推荐阅读