search

Home  >  Q&A  >  body text

python在写多线程有个疑问

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<code>

from threading import Thread

 

class CountdownThread(Thread):

    def __init__(self, n):

        super(CountdownThread, self).__init__()   #在继承Thread时,为什么要执行Thread的构造函数呢?

        self.n = 0

    def run(self):

        while self.n > 0:

            print('T-minus', self.n)

            self.n -= 1

            time.sleep(5)

c = CountdownThread(5)

c.start()</code>

1.在继承Thread时,为什么要执行Tread的构造函数呢?

ps:本人背景自学+google,还请不吝赐教

PHPzPHPz2887 days ago358

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:29:11

    The constructor of the parent class has thread initialization work.

    For example: when a disciple inherits a teacher’s profession, he can’t just innovate right from the start, right? You still have to learn the basic skills from the master

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:29:11

    If the constructor is overridden in a subclass, the constructor of the subclass will be called. If not, the constructor of its parent class will be called. If the constructor of the parent class is not called in the constructor of the subclass. Then the constructor of the parent class will not be called in the child class

    reply
    0
  • Cancelreply