首页 > 解决方案 > 如何从python3中的类外访问类内函数的变量

问题描述

这是python3.7中的示例代码

code

class MyClass(object):
    def xxx(self):
        self.a = 10
        self.b = 15
        self.c = 20

    def yyy(self):
        self.xxx()
        print(self.a)
        print(self.b)

#Create an instance of the class
obj = MyClass()

obj.yyy() #this will print value of a and b

code

我想要函数 xxx() 内的类之外的 a 和 b 的值,例如

obj = MyClass()
x = obj.xxx()
print(x.a)
print(x.b)

但这会产生错误: AttributeError: 'NoneType' object has no attribute 'a'

标签: python-3.x

解决方案


在此处输入图像描述

这将帮助你。这就是为什么你的情况是不可能的。


推荐阅读