Reputation: 13
this is the code:
class Empty(Exception):
pass
class ArrayStack:
def __init__(self):
self._data = []
def __len(self):
return len(self._data)
def is_empty(self):
return len(self._data)==0
def push(self, e):
self._data.append(e)
def pop(self):
if self.is_empty():
raise empty('Stack is Empty')
return self._data.pop()
def top(self):
if self.is_empty():
raise empty('Stack is Empty')
return self._data[-1]
s= ArrayStack()
s.push(10)
s.push(20)
print('Stack: ', s._data)
**print('Length: ', len(s)) #this line is throwing the problem**
print('Is-Empty: ', s.is_empty())
print('Popped: ', s.pop())
print('Stack: ', s._data)
error message
Traceback (most recent call last): File "main.py", line 31, in print('Length: ', s.__len()) AttributeError: 'ArrayStack' object has no attribute '__len'
Upvotes: 1
Views: 886
Reputation: 1626
In your ArrayStack Class your method __len() is typo mistake.
Your method should be len() because it is a magic method.
def __len__(self):
return len(self._data)
Upvotes: 2
Reputation: 92
You have a typo in your len definition. It should be __len__. I have noticed a second issue. You should capitalize 'empty' raise.
Upvotes: 0