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.
Your subclass's
__init__
makes no sense.You should instead put:
Then customize
__str__
or__repr__
if you want to change how the subclass is displayed.