我想将方法值传递给同一个类的其他方法
class abc:
def me(self):
self.x = 5
def you(self):
print('I want to print here -> x = 5')
if __name__ == '__main__':
SR =abc()
SR.you()
在调用方法'你'时我怎样才能打印x的值,这是其他方法的变量
永远不会调用你的'me'方法,因此永远不会定义x。如果您不想使用init,可以在“you”方法中调用“我”方法,以便定义x。
class abc:
def me(self):
self.x = 5
def you(self):
self.me()
print(self.x)
if __name__ == '__main__':
SR = abc()
SR.you()
使用__init__
:
class abc:
def __init__(self):
self.x = 5
def you(self):
print('I want to print here -> x ={}'.format(self.x))
if __name__ == '__main__':
SR =abc()
SR.you()