Python 调用类后在initialize中运行方法
我希望方法测试()的结果存储在变量a中。这样我就可以在我的课堂之外访问它Python 调用类后在initialize中运行方法,python,python-3.x,oop,Python,Python 3.x,Oop,我希望方法测试()的结果存储在变量a中。这样我就可以在我的课堂之外访问它 class test: def __init__(self): a = method() def method_test(): return "working" check = test print(check.a) 您需要将其设置为属性,这可以通过self.a: class test: def __init__(self): self.a =
class test:
def __init__(self):
a = method()
def method_test():
return "working"
check = test
print(check.a)
您需要将其设置为属性,这可以通过
self.a
:
class test:
def __init__(self):
self.a = test.method_test()
def method_test():
return "working"
check = test()
print(check.a)
#working
self.a=method\u test()
?类型对象“test”没有属性“a”@meowgoesthedog我得到一个错误你是说check=test
(类型对象)还是check=test()
(实例)?后者应该有效。@meowgoesthedogSeeyatu
的答案中没有定义“方法测试”。您的代码错误太多,无法一次调试一条注释。如果有帮助,请不要忘记接受答案,谢谢!
class test:
def __init__(self):
self.a = self.method_test()
def method_test(self):
return "working"
check = test()
print (check.a)