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.

3 Years
Discussion Span
Last Post by james.lu.75491856


python 3:


put __ around the bold ones

Edited by pyTony: inline code tags

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.