python-3.x - 如何从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 - 从 2 个 uint64 值中提取 Spooky-hash 128 位值
- r - 使用 R 并行处理 XML 节点
- javascript - 不能在 Typescript 和 Webpack 项目中使用 square/js-jose (jose-jwe-jws) npm 包
- php - Symfony 3 - 我在管理我的角色时遇到了一些困难
- powershell - 移除所有 Sitecore 节点的发布限制
- lightbox2 - 灯箱图像 X 关闭按钮 对齐图像右上角
- reporting-services - SSRS 报告行可见性在不应该显示详细记录时显示
- javascript - 验证 var 是否存在
- c# - 需要模式实现的建议以将“处理器”从 WebApp 注入 NuGet
- javascript - Python EEL:在脚本运行期间更改按钮标签