python class inheritance with separate script files

1.9k Views Asked by At

I'm having an issue with my subclass not inheriting superclass attributes when the classes are in separate files. When I run my main bot.py I get an error stating:

AttributeError: 'Serv' object has no attribute 'server'

Here is my example:

file 1 [bot.py]

import commands

class Bot(object):
    def __init__(self):
        self.server  = "myserver.domain.com"

    def getserv(self):
        return self.server

if __name__ == "__main__":
    print( commands.Serv() )

file 2 [commands.py]

from bot import Bot

class Serv(Bot):
    def __init__(self):
        return self.getserv()

I'm somewhat new to object inheritance in python and am sure this is a simple issue that I'm overlooking. Any help identifying my problem would be greatly appreciated! Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

Your subclass's __init__ makes no sense.

You should instead put:

from bot import Bot

class Serv(Bot):
    def __init__(self):
        super().__init()
        self.something_else = whatever

Then customize __str__ or __repr__ if you want to change how the subclass is displayed.