python - 父类(super class) __init__ 不认识它的 kwargs

标签 python multithreading subclass keyword-argument

我正在尝试使用显示为 an answer to another questionStoppableThread 类:

import threading

# Technique for creating a thread that can be stopped safely
# Posted by Bluebird75 on StackOverflow
class StoppableThread(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""

    def __init__(self):
        super(StoppableThread, self).__init__()
        self._stop = threading.Event()

    def stop(self):
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

但是,如果我运行类似的东西:

st = StoppableThread(target=func)

我得到:

TypeError: __init__() got an unexpected keyword argument 'target'

可能是对如何使用它的疏忽。

最佳答案

StoppableThread 类不会在构造函数中采用或传递任何附加参数给 threading.Thread。你需要做这样的事情:

class StoppableThread(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""

    def __init__(self,*args,**kwargs):
        super(threading.Thread,self).__init__(*args,**kwargs)
        self._stop = threading.Event()

这会将位置参数和关键字参数传递给基类。

关于python - 父类(super class) __init__ 不认识它的 kwargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15462486/

相关文章:

Swift, "subclass"UITableView 的数据源方法不知何故?

Python 默认继承?

python - 如何更新 MySQL 中的列

c++ - QtConcurrent::run 异常通知

java - 在 Java 中重用 ObjectOutputStreams

Swift:覆盖也是父类属性类的子类的子类属性

c# - 将来自同一基类的不同子类放入列表 C#

python - 字典理解 Python

python - 尝试打开()文件时出现类型错误: 'newline' is an invalid keyword argument for this function,

java - Java run() 方法如何工作?