Home > Software engineering >  Python OOB: Not understanding subclasses
Python OOB: Not understanding subclasses

Time:09-25

I am currently working on the 3.2.1.10 A short journey from procedural to object approachlab from edube.org, course (Python Essentials 2 (Intermediate, v.2.0).

The task is about programming a stack in object oriented style. We have a push and a pop method so far and a simple stack we can fill and take away the last item. Now it should be extended to be able to display the sum of the values in the stack. The complete code given in the lab is as follows:

class Stack:
    def __init__(self):
        self.__stack_list = []

    def push(self, val):
        self.__stack_list.append(val)

    def pop(self):
        val = self.__stack_list[-1]
        del self.__stack_list[-1]
        return val


class AddingStack(Stack):
    def __init__(self):
        Stack.__init__(self)
        self.__sum = 0

    def get_sum(self):
        return self.__sum

    def push(self, val):
        self.__sum  = val
        Stack.push(self, val)

    def pop(self):
        val = Stack.pop(self)
        self.__sum -= val
        return val


stack_object = AddingStack()

for i in range(5):
    stack_object.push(i)
print(stack_object.get_sum())

for i in range(5):
    print(stack_object.pop())

The code works. As an explanation for using class AddingStack(Stack) it says:

We don't want to modify the previously defined stack. It's already good enough in its applications, and we don't want it changed in any way. We want a new stack with new capabilities. In other words, we want to construct a subclass of the already existing Stack class.

The first step is easy: just define a new subclass pointing to the class which will be used as the superclass.

This is what it looks like: class AddingStack(Stack): pass

The class doesn't define any new component yet, but that doesn't mean that it's empty. It gets all the components defined by its superclass

However, when I run the same code, but just modify the line to:

class AddingStack():

it still works. I don't understand what the benefit of class AddingStack(Stack) is?

CodePudding user response:

However, when I run the same code, but just modify the line to:

class AddingStack():

it still works. I don't understand what the benefit of class AddingStack(Stack) is?

It still works because the methods in AddingStack explicitly call other methods in Stack, which defeats the entire point of inheritance.

CodePudding user response:

Usually the benefit from inheritance in OOP is the ability to crate a class from an existing class, and modify it a bit with ease.

If you really just override every single function in the super-class, then no, don’t use inheritance, it won’t benefit you nothing.

It is very useful in cases when you have a sub-class that only change some of the functions and the things from the super-class, and the rest, will be using the super-class functions.

CodePudding user response:

It works because you are calculating the sum without actually using the elements of the stack, instead accumulating the result in the __sum variable.

You are also not using inheritance, instead delegating to the pop() and push() methods of class Stack.

The objective of the exercise seems to be for you to add up the elements of the stack (which is already implemented in the superclass) and to implement get_sum() such that you iterate through the list of values on the stack and add them up.

  • Related