class testing():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def house(self):
d = self.a+self.b+self.c
print d
module="hello"
p = testing(1, 2, 3)
p.house()
如何从我的 testing 类中访问 module 变量?我知道我可以通过执行以下操作将它作为参数添加到类构造函数中:
p=testing(1,2,3,module)
但除非迫不得已,否则我不想这样做。我还可以通过哪些其他方式从 testing 类中访问 module 变量?
请您参考如下方法:
您只需引用它;您不需要任何特殊的全局权限即可访问它。这不是最好的方法,但由于您没有描述您的应用程序和模块化要求,我们现在所能做的就是解决您眼前的问题。
顺便说一句,您的 a、b、c 引用不正确。见下文。
class testing():
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
self.greeting = module
def house(self):
d = self.a + self.b + self.c
print d
print self.greeting
module="hello"
p = testing(1, 2, 3)
p.house()
输出:
6
hello
