Hi all, I'm quite new to Python (and programming in general) and I'm a bit confused about how to implement inheritance in an assignment I'm doing at the moment.

The basic gist is: we're supposed to make a program that can record stock items for a company. We're to make a super class called StockItem which records the unique ID, client who ordered, price per unit and warehouse number. But we're also to make two specialised stock items, StockBook and StockCD, which inherit from StockItem.

The init for my StockItem class is as follows:

def __init__(self, uniqueID=0, clientName = "unknown", pricePerUnit = 0.0, warehouseNumber = 1):
        self.uniqueID = uniqueID
        self.clientName = clientName
        self.pricePerUnit = pricePerUnit
        self.warehouseNumber = warehouseNumber

But when I'm making my init for my subclasses, I'm not sure how to call the init of the superclass. Basically what I'd like to have is to be able to generate instances of StockBook and StockCD that have their specialised attributes but also the general ones given in the init of the superclass.

If I include StockItem.init(etc) in the init of the subclass, there still doesn't seem to be a way of passing the attributes like unique ID into the subclass instances. I've thought of simply writing all the required attributes into the init of the subclass, but this seems rather wasteful... I was sure there would be a more efficient solution. Does anyone know a way around this?

I hope I've expressed my problem clearly enough, thanks in advance.

Recommended Answers

All 3 Replies

super(YOUR_SUBCLASS_NAME,self).__init__()

super(self).__init__()

python 3:

super().__init__()

put __ around the bold ones

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.