所以我整天都在乱搞这个问题,而我仍然无法让它工作
class pleaseWork:
def __init__(self):
self.foo=printThis(1)
self.bar=printThis(2)
def printThis(x):
if x==1:
print "foot"
elif x==2:
print "bar"
result=pleaseWork()
result.bar
它只返回
NameError: global name 'printThis' is not defined
请告诉我为什么它不起作用......
答案 0 :(得分:5)
因为printThis
是当前对象的属性。
self.foo = self.printThis(1)
答案 1 :(得分:1)
class pleaseWork:
def __init__(self):
self.foo=self.printThis(1)
self.bar=self.printThis(2)
def printThis(self, x):
if x==1:
print "foot"
elif x==2:
print "bar"
result=pleaseWork()
result.bar