Python classes exercise unknown error

I am trying to do an excersise that involves Classes and Class inheritance In Python.

I keep having problems with trying to add the SpecialInfo input to the values for the fruits. The error that I’m getting is (“SpecialItem” object has no attribute “SpecialInfo”).

I would be very grateful if anyone could lend me a hand with this.

The code is shown here:

class Item():
    
    def __init__(self, Description, Number, UnitPrice):
        self.Description = Description
        self.Number = Number
        self.UnitPrice = UnitPrice
        print("Create new item: ")
        self.PrintItemInfo()
        
    
    def PrintItemInfo(self):
        print(self.Description, self.Number, "£", self.UnitPrice)
    def __str__(self):
        return self.Description

class SpecialItem(Item):
    
    def __init__(self,Description, Number, UnitPrice, SpecialInfo):
        super().__init__(Description,Number,UnitPrice)
        self.SpecialInfo = SpecialInfo
        print(SpecialInfo)
        
    def __str__(self, Description,SpecialInfo):
        return self.Description, self.SpecialInfo
    
    def PrintItemInfo(self):
        print(self.Description, self.Number,self.UnitPrice, self.SpecialInfo)
        
    
        
Apple = Item("Apple", 1, 0.5)
Apple.PrintItemInfo()
Banana = Item("Banana", 6, 3)
Carrot = Item("Carrot", 8, 2)
Oranges = SpecialItem("Orange", 3, 4, "they are orange")
ShoppingList = [Apple, Banana, Carrot, Oranges]
print("Total shopping list: ")
Sum = 0
for i in ShoppingList:
   x = i.Number * i.UnitPrice
   Sum += x
   print(i.Description,i.Number, "£", x)
   
       
print("£", Sum)

The problem starts here in the constructor of SpecialItem :

super().__init__(Description,Number,UnitPrice)

This super call will constructor of Item class, which in turn will call printItemInfo:

self.PrintItemInfo()

which call the PrintItemInfo method of child class:

  def PrintItemInfo(self):
        print(self.Description, self.Number,self.UnitPrice, self.SpecialInfo)

but we have not yet set/initialized self.SpecialInfo

as such, you get an error.

You could step through your code here:

http://www.pythontutor.com/visualize.html#mode=edit

if you find it difficult to describe the flow I just described.

1 Like

Got it, Thanks for all the help!!

the take away from my answer should no be the answer/solution, but instead how to go about debugging this

I know the stack-trace might look terrifying:

Traceback (most recent call last):
  File "<string>", line 35, in <module>
  File "<string>", line 19, in __init__
  File "<string>", line 8, in __init__
  File "<string>", line 27, in PrintItemInfo
AttributeError: 'SpecialItem' object has no attribute 'SpecialInfo'

but precisely tells us the problem:

line 35: Oranges = SpecialItem("Orange", 3, 4, "they are orange")
line 19: super().__init__(Description,Number,UnitPrice)
line 8: self.PrintItemInfo()
line 27: print(self.Description, self.Number,self.UnitPrice, self.SpecialInfo)

so pretty useful :slight_smile: The stack-trace even tells what has been execute (module -> script/file currently running), we can see both init methods being called.

Combining that with inserting print statements or using a debugger (like python tutor) is valuable skill to learn :slight_smile:

1 Like