0

I need to make this python 3 code python 2 compatible:

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

I get this error when I try to use the class in 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)
theonlygusti
  • 11,032
  • 11
  • 64
  • 119

1 Answers1

2

You'll need to pass in the current class and self:

super(AlarmThread, self).__init__()

Also see Why is Python 3.x's super() magic? why and how Python 3 dropped the need to specify those.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • As a heads up, you may get a TypeError using super. The solution is to add "object" to the list of parent objects in the class definition. So if "class MyClass(otherclass):" doesn't work, try: "class MyClass(otherclass,object):" – RufusVS Aug 05 '17 at 23:20
  • @RufusVS `super()` only works with new-style classes, yes. – Martijn Pieters Aug 05 '17 at 23:30