arin
arin

Reputation: 3

How to correctly inherit class from another file

I have 2 files. driver.py and stack.py. I would like to inherit the base class stack.py's 2 functions into driver.py, but I am having issues with actually creating the class (encounter attribute errors while running). The code is supposed to ask for user input until 'end' is received and the program should output all the input.

Please disregard the ill-formatting, I had issues including the 'import' lines in the snippets.

class Stack:

def __init__(self):
    '''A new empty stack'''
    self.items = []

def push(self, o):
    '''Make o the new top item in this Stack.'''
    self.items.append(o)
    
def pop(self):
    '''Remove and return the top item.'''
    return self.items.pop()

def peek(self):
    '''Return the top item.'''
    return self.items[-1]

def isEmpty(self):
    '''Return whether this stack is empty.'''
    return self.items == []

def size(self):
    '''Return the number of items in this stack.'''
    return len(self.items)

class UpStack:


def __init__(self):
    '''A new empty stack'''
    self.stack = []

def push(self, o):
    '''Make o the new top item in this Stack.'''
    self.stack.insert(0, o)
    
def pop(self):
    '''Remove and return the top item.'''
    return self.stack.pop(0)

def peek(self):
    '''Return the top item.'''
    return self.stack[0]

def isEmpty(self):
    '''Return whether this stack is empty.'''
    return self.stack == []

def size(self):
    '''Return the number of items in this stack.'''
    return len(self.stack)

from stack import *

if __name__ == '__main__':
#-----------------------------------Stack Code-----------------------------------

                    s = Stack()
                    s.push('Hello')
                    print(s.pop())

                    data = ""
                    while data.lower() != 'end':
                            data = input("Enter Command: ")
                            if data.lower() != 'end':
                                    s.push(data)

                    while not s.isEmpty():
                            print(s.pop())

    #-----------------------------------UpStack Code-----------------------------------

                    u = UpStack()
                    u.push('Hello')
                    print(u.pop())

                    data = ""
                    while data.lower() != 'end':
                            data = input("Enter Command: ")
                            if data.lower() != 'end':
                                    u.push(data)

                    while not u.isEmpty():
                            print(u.pop())

from stack import *

    class Test:
	#-----------------------------------Stack Code-----------------------------------
                def stacker(self):
                        s = Stack()
                        s.push('Hello')
                        print(s.pop())

                        data = ""
                        while data.lower() != 'end'
                        data = input("Enter Command: ")
                                if data.lower() != 'end':
                                        s.push(data)

                                while not s.is_empty():
                                        print(s.pop())

        #-----------------------------------UpStack Code-----------------------------------
                def upstacker(self):
                        u = UpStack()
                        u.push('Hello')
                        print(u.pop())

                        data = ""
                        while data.lower() != 'end':
                        data = input("Enter Command: ")
                                if data.lower() != 'end':
                                        u.push(data)

                                while not u.is_empty():
                                        print(u.pop())

Since I don't see anything when I actually run the code, I make an instance of it and this is what I get.

    >>> s = Stack()
    >>> s
    <stack.Stack object at 0x103bb3a58>
    >>> s.push(2)
    >>> s.stacker()
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        s.stacker()
    AttributeError: 'Stack' object has no attribute 'stacker'

Upvotes: 0

Views: 478

Answers (1)

levi
levi

Reputation: 22697

That is because Stack() instance does not have stacker() method.

stacker method belongs to your Test class.

instead of typing

>>> s = Stack()
>>> s.stacker()

you should use

>>> t = Test()
>>> t.stacker()

Upvotes: 1

Related Questions