class Main():
def __init__(self):
self.A = 'apples' #A is now an instance variable, set to 'apples'
#don't forget parentheses here!
def banana():
B = 'banana'
m = Main() #you'll need an instance of main to reference a variable in it...
print(B + m.A) #m.A gets the variable A from m.
与您的示例更相似,但需要更多代码(使用函数获取变量):
class Main():
def __init__(self):
self.A = 'apples' #A is now an instance variable, set to 'apples'
def LOL(self):
return self.A
#don't forget parentheses here!
def banana():
B = 'banana'
m = Main() #you'll need an instance of main to reference a variable in it...
print(B + m.LOL()) #m.LOL() will return m.A
与您的示例更相似,但需要更多代码(使用函数获取变量):
相关问题 更多 >
编程相关推荐