sadmicrowave
sadmicrowave

Reputation: 40892

python class inheritance with separate script files

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.

Upvotes: 0

Views: 1923

Answers (1)

o11c
o11c

Reputation: 16046

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.

Upvotes: 1

Related Questions