I am trying to code my first Queue class. So far I have this code that seemes to work:
class Queue(list):
def __init__(self):
self = []
def insert(self, x):
self.append(x)
return self
def delete(self):
if len(self) == 0:
print "The queue is empty"
else:
self.remove(self[0])
return self
However, I was recomended to rewrite it, and when I try something like this I got wrong results:
class Queue:
def __init__(self):
self.items = []
def insert(self, x):
self.items.append(x)
Test:
queue = Queue()
print queue
queue.insert(5)
print queue
Got:
<__main__.Queue instance at 0x0000000002A2F148>
<__main__.Queue instance at 0x0000000002A2F148>
Could you, please, explain me the difference between two approaches and why the second doesn't work (although I saw it on many websites)?
You need need implement either str or repr for your class Queue before printing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With