-3

So for learning python and just in general some data structures, I am creating a Stack to start with. This is a simple array-based stack.

Here is my code:

class ArrayBasedStack:
    'Class for an array-based stack implementation'

    def __init__(self):
        self.stackArray = []

    def pop():
        if not isEmpty():
            # pop the stack array at the end
            obj = self.stackArray.pop()
            return obj
        else:
            print('Stack is empty!')
            return None

    def push(object):
        # push to stack array
        self.stackArray.append(object)

    def isEmpty():
        if not self.stackArray: return True
        else: return False

'''
    Testing the array-based stack
'''

abs = ArrayBasedStack()
abs.push('HI')
print(abs.pop())

But I'm getting this error:

Traceback (most recent call last):

File "mypath/arrayStack.py", line 29, in abs.push('HI')

TypeError: push() takes 1 positional argument but 2 were given [Finished in 0.092s]

madcrazydrumma
  • 1,847
  • 3
  • 20
  • 38

1 Answers1

1

You are missing self parameter.

self is a reference to an object. It's very close to the concept of this in many C style languages.

So you can fix like this.

class ArrayBasedStack:
    'Class for an array-based stack implementation'

    def __init__(self):
        self.stackArray = []

    def pop(self):
        if not self.isEmpty():
            # pop the stack array at the end
            obj = self.stackArray.pop()
            return obj
        else:
            print('Stack is empty!')
            return None

    def push(self, object):
        # push to stack array
        self.stackArray.append(object)

    def isEmpty(self):
        if not self.stackArray: return True
        else: return False

'''
    Testing the array-based stack
'''

abs = ArrayBasedStack()
abs.push('HI')
print(abs.pop())

Output:

HI
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42