Sourav
Sourav

Reputation: 504

Attribute error while inheritance

I am trying to inherit a variable from parent class to child class. However, I am facing an attribute error. Please find my code below :-

class Stack1(object):
    def __init__(self):
        self.stack1 = []

    def push(self, item):
        self.stack1.append(item)
    def pop(self):
        self.popped_value = self.stack1.pop()       
        print("popped_value parent", self.popped_value)
    def peek(self):
        try:
            return self.stack1[len(stack1)-1]
        except:
            print("Cannot peek into stack 1")
    def is_empty(self):
        if len(self.stack1) == 0:
            return True
        else:
            return False
    def display(self):
        print(self.stack1)

class Stack2(Stack1):
    def __init__(self):
        self.stack2 = []        

    def push(self):
        #popped_value = Stack1.pop(self)
        #popped_value = super(Stack2, self).pop()
        super(Stack2, self).pop()
        print("popped_value child", self.popped_value)
        self.stack2.append(popped_value)
    def pop(self):
        return self.stack2.pop()
    def peek(self):
        try:
            return self.stack2[len(stack2)-1]
        except:
            print("Cannot peek into stack 2")
    def is_empty(self):
        if len(self.stack2) == 0:
            return True
        else:
            return False
    def display(self):
        print(self.stack2)

first_stack = Stack1()
second_stack = Stack2()

first_stack.push(1)
first_stack.push(2)
first_stack.display()
print("Pushed above items into stack1")
first_stack.pop()
second_stack.push()
second_stack.display()
print("Pushed above items into stack2")
first_stack.pop()
second_stack.push()
second_stack.display()
print("Pushed above items into stack2")

Below is the error :-

E:\>python dsq.py
[1, 2]
Pushed above items into stack1
popped_value parent 2
Traceback (most recent call last):
  File "dsq.py", line 56, in <module>
    second_stack.push()
  File "dsq.py", line 30, in push
    super(Stack2, self).pop1()
  File "dsq.py", line 8, in pop1
    self.popped_value = self.stack1.pop()
AttributeError: 'Stack2' object has no attribute 'stack1'

Here, I am trying to implement a queue using two stacks. So, I am trying to push the popped item from first stack to second stack. So, as to achieve the same, I need to access the popped_item from the first stack to my child class Stack2.

Could you please help me out here ?

Upvotes: 0

Views: 49

Answers (1)

Python is rather peculiar in the way that the parent initializer (Stack1.__init__) is not called automatically for derived classes; you need to make sure of it yourself:

class Stack2(Stack1):
    def __init__(self):
        super().__init__()  # call the __init__ method of the super class
        self.stack2 = []        

And by the way, you do not inherit just 1 variable, you inherit all of the behaviour of parent class that you did not override in the child.

Upvotes: 2

Related Questions