类中的Thread .__ init __(self)如何工作? [英] How does Thread.__init__(self) in a class work?

查看:216
本文介绍了类中的Thread .__ init __(self)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我找到了这段代码:

So I found this code:

from threading import Thread
class Example(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run (self):
        print("It's working!")
Example().start()

它显示正在运行!"使用另一个线程,但这如何工作?我在类中找不到有关Thread .__ init __(self)的任何信息.它与超类有关吗?

And it prints "It's working!" using another thread, but how does this work? I can't find anything about Thread.__init__(self) in a class. Does it have something to do with superclasses?

推荐答案

您的__init__方法是完全多余的.您实际上是用自己的实现替换Thread.__init__(),而您自己的实现只是调用Thread.__init__().如果将其删除,则什么都不会改变:

Your __init__ method is entirely redundant. You are in effect replacing Thread.__init__() with your own implementation, where your own implementation simply calls Thread.__init__(). If you removed it, nothing would change:

class Example(Thread):
    def run (self):
        print("It works!")

您的Example.run()方法被简单地调用是因为您使用

Your Example.run() method is simply called because you started the thread using the Thread.start() method:

start()
开始该线程的活动.

start()
Start the thread’s activity.

每个线程对象最多只能调用一次.它安排在单独的控制线程中调用对象的run()方法.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

另请参见 Thread.run()文档 :

Also see the Thread.run() documentation:

run()
表示线程活动的方法.

run()
Method representing the thread’s activity.

您可以在子类中重写此方法.标准的run()方法调用传递到对象构造函数的可调用对象作为目标参数(如果有),并分别从args和kwargs参数中获取顺序参数和关键字参数.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

您的__init__方法与此无关.

现在,如果您在Thread子类中创建了__init__方法,然后 not 确保已调用Thread.__init__,则可以防止该类设置重要的实例信息,从而破坏实例:

Now, if you created a __init__ method in a Thread subclass and then did not make sure Thread.__init__ was called, then you prevented the class from setting important instance information, breaking instances:

>>> from threading import Thread
>>> class Example(Thread):
...     def run (self):
...         print("It works!")
... 
>>> Example().start()
It works!

>>> class BrokenExample(Thread):
...     def __init__(self):
...         # not doing anything
...         pass
...     def run (self):
...         print("It works!")
... 
>>> BrokenExample().start()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python2.7/threading.py", line 737, in start
    raise RuntimeError("thread.__init__() not called")
RuntimeError: thread.__init__() not called

因为这是一个常见错误,所以Thread.start方法会引发一个自定义异常,以明确告诉您Thread.__init__未执行.

Because this is a common error to make, the Thread.start method throws a custom exception to tell you explicitly that Thread.__init__ was not executed.

这篇关于类中的Thread .__ init __(self)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆