我试图用python理解面向对象的编程.我是编程新手.我有这个课程给我一个我不理解的错误,如果有人能为我提供更多的信息,我将很高兴:
class TimeIt(object):
def __init__(self, name):
self.name = name
def test_one(self):
print 'executed'
def test_two(self, word):
self.word = word
i = getattr(self, 'test_one')
for i in xrange(12):
sleep(1)
print 'hello, %s and %s:' % (self.word, self.name),
i()
j = TimeIt('john')
j.test_two('mike')
Run Code Online (Sandbox Code Playgroud)
如果我上这堂课,我就得到了 'int' object is not callable" TypeError
但是,如果我在iwith self(self.i)之前,它可以工作.
class TimeIt(object):
def __init__(self, name):
self.name = name
def test_one(self):
print 'executed'
def test_two(self, word):
self.word = word
self.i = getattr(self, 'test_one')
for i in xrange(12):
sleep(1)
print 'hello, %s and %s:' % (self.word, self.name),
self.i()
Run Code Online (Sandbox Code Playgroud)
我的问题是,是不是i = getattr(self, 'test_one')将test_one函数赋给了i?
怎么i()不行?
为什么self.i()工作?
为什么i一个int(因此'int' object is not callable TypeError)?
那是很多问题.提前致谢