首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >超类__init__不识别它的kwargs

超类__init__不识别它的kwargs
EN

Stack Overflow用户
提问于 2013-03-17 15:25:38
回答 2查看 1.5K关注 0票数 4

我试图使用StoppableThread类表示为an answer to another question

代码语言:javascript
复制
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()

但是,如果我运行这样的操作:

代码语言:javascript
复制
st = StoppableThread(target=func)

我得到:

TypeError:__init__()得到了一个意想不到的关键字参数“目标”

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

EN

Stack Overflow用户

回答已采纳

发布于 2013-03-17 15:27:55

StoppableThread类不接受或传递构造函数中的任何附加参数给threading.Thread。你需要这样做,而不是这样:

代码语言:javascript
复制
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()

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

票数 5
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15462486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档