I have created a class Queue with a constructor that takes a list as an argument with an empty list as default. Though object q3 is created using no arguments, the default value going inside the constructor is of object q2.
I am using Python 3.8.5 on Ubuntu 20.04 LTS Virtual machine.
class Queue:
def __init__(self,elements = []):
self.elements = elements
print(elements)
def enqueue(self,element):
self.elements.append(element)
def dequeue(self):
return self.elements.pop(0)
def peek(self):
if len(self.elements)>0:
return self.elements[0]
else:
return None
q1 = Queue(["Q1_abc","Q1_def","Q1_ghi"])
q2 = Queue()
q2.enqueue("Q2_abc")
q2.enqueue("Q2_def")
q2.enqueue("Q2_ghi")
q3 = Queue()
q3.enqueue("Q3_abc")
print(q3.elements)
q3 = Queue()
q3.enqueue("Q3_abc")
print(q3.elements)
Output:
['Q1_abc', 'Q1_def', 'Q1_ghi']
[]
['Q2_abc', 'Q2_def', 'Q2_ghi']
['Q2_abc', 'Q2_def', 'Q2_ghi', 'Q3_abc']