我希望将 method_test() 的结果存储在变量 a 中。这样我就可以在课堂之外访问
class test:
def __init__(self):
a = method()
def method_test():
return "working"
check = test
print(check.a)
答案 0 :(得分:2)
您需要将其设置为属性,可以使用self.a
:
class test:
def __init__(self):
self.a = test.method_test()
def method_test():
return "working"
check = test()
print(check.a)
#working
答案 1 :(得分:1)
class test:
def __init__(self):
self.a = self.method_test()
def method_test(self):
return "working"
check = test()
print (check.a)