user9018881
user9018881

Reputation:

Python Inheritance initialize problems

I have threading class in serverThread.py file as shown:

import threading

class serverThread(threading.Thread):
    def __init__(self, name):
        try:
            threading.Thread.__init__(self)
            self.name = name
        except:
            exit()

    def run(self):
           print("Hello")

I created a new project.I want to inherit class from above class as shown:

import serverThread


class tcpThread(serverThread):
    def __init__(self, name):
        serverThread.__init__(self,name)

    def run():
        serverThread.run(self)


t1 = tcpThread("Tcp Server")
t1.start()

When I run this script gives me error:

Error: Traceback (most recent call last): File "serverTcpThread.py", line 4, in <module> class tcpThread(serverThread): TypeError: module.__init__() takes at most 2 arguments (3 given)

Upvotes: 1

Views: 183

Answers (1)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140276

The error you're reporting is probably because the base class is imported from a bad path, cannot reproduce here.

That said, there's another (similar) error: when redefining the run method, you have to pass the self parameter

class tcpThread(serverThread):
    def __init__(self, name):
        serverThread.__init__(self,name)

    def run(self):
        serverThread.run(self)

the code runs fine after that. note that there's no need to redefine the run method only to call the parent method.

Upvotes: 2

Related Questions