简体   繁体   English

面向对象的Python编程

[英]Python Object Oriented Programming

Am trying to understand object oriented programming with python. 我试图用python理解面向对象的编程。 Am new to programming. 我是编程新手。 I have this class that is giving me an error I don't understand and I will be glad if anyone can throw more light on this for me: 我有这个课程给我一个我不理解的错误,如果有人能为我提供更多的信息,我将很高兴:

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')

If I run this class I get 'int' object is not callable" TypeError 如果我运行这个类,我得'int' object is not callable" TypeError

However, if I precede the i with self ( self.i ), it works. 但是,如果我在i之前使用selfself.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()

My question is, doesn't i = getattr(self, 'test_one') assign the test_one function to i ? 我的问题是,不是i = getattr(self, 'test_one')将test_one函数分配给i
How come i() doesn't work? 为什么i()不起作用?
Why does self.i() work? 为什么self.i()有效?
Why is i an int (hence the 'int' object is not callable TypeError )? 为什么是iint (因此'int' object is not callable TypeError )?
That's a lot of questions. 那是很多问题。 Thanks in advance 提前致谢

You're overwriting i within loop. 你在循环中覆盖i When you're "preceding" i with self , you're creating different variable, which is not overwritten. 当你在“ i self ”前面时,你正在创建不同的变量,这个变量不会被覆盖。

@SilentGhost is right on the money with his answer. @SilentGhost对他的回答是肯定的。

To illustrate, try chaning the test_two method to this: 为了说明,尝试将test_two方法转换为:

def test_two(self, word):
    self.word = word
    i = getattr(self, 'test_one')
    for some_other_variable_besides_i in xrange(12):
        sleep(1)
        print 'hello, %s and %s:' % (self.word, self.name),
        i()

Your code overwrite the variable i (set as a method) within the for loop (see comments) 您的代码覆盖for循环中的变量i(设置为方法)(请参阅注释)

def test_two(self, word):
    self.word = word
    i = getattr(self, 'test_one')
    # i is now pointing to the method self.test_one
    for i in xrange(12):
        # now i is an int based on it being the variable name chosen for the loop on xrange
        sleep(1)
        print 'hello, %s and %s:' % (self.word, self.name),
        i()

In addition, you certainly don't need to assign the test_one method to a variable like i . 此外,您当然不需要将test_one方法分配给像i这样的变量。 Instead, you can just call the method replacing 相反,您可以调用替换方法

i()

with

self.test_one()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM