如何在python 2中启动super

时间:2016-12-31 15:01:41

标签: python python-2.7

我需要使这个python 3代码python 2兼容:

class AlarmThread(threading.Thread):
    def __init__(self, file_name="beep.wav"):
        super().__init__()
        self.file_name = file_name
        self.ongoing = None

当我尝试在python 2中使用该类时出现此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "plugin.py", line 144, in run
    alert_after_timeout(seconds, message)
  File "plugin.py", line 96, in alert_after_timeout
    thread = AlarmThread()
  File "plugin.py", line 78, in __init__
    super().__init__()
TypeError: super() takes at least 1 argument (0 given)

1 个答案:

答案 0 :(得分:2)

您需要传入当前班级self

super(AlarmThread, self).__init__()

另见Why is Python 3.x's super() magic?为什么以及Python 3如何放弃指定这些内容的需要。