0

so I have a basic constructor for my queue class as seen here:

class Queue:
  def __init__(self):
    self._qhead = None
    self._qtail = None
    self._count = 0

but I need to allow the constructor to take an optional parameter that indicates the max size of the queue and if no size is given, then the queue is unbounded.

How would I do that? TIA

buchelle
  • 35
  • 1
  • 4
  • Could look at how to implement optional args https://stackoverflow.com/questions/9539921/how-do-i-create-a-python-function-with-optional-arguments – gom1 Oct 17 '19 at 18:20
  • Optionally just add an extra parameter `def __init__(self, max_size):` for positional or `def __init__(self, max_size=100):` for keyword argument - change 100 to whatever you think is right. – dmitryro Oct 17 '19 at 18:20

2 Answers2

0
class Queue:
  def __init__(self, size=Value):
    self._qhead = None
    self._qtail = None
    self._count = 0

You would set a default by setting a parameter equal to whatever you want it to be. If an object is created with another argument, it would replace the default.

Diego
  • 216
  • 1
  • 11
  • that's what i assumed, but what would i need to set the parameter equal to ensure that the list was otherwise unbounded? – buchelle Oct 17 '19 at 18:33
  • Lets say you set `size is None`, you would have an if statement for when `size` is set to `None` having it take a different path in your class – Diego Oct 17 '19 at 18:35
0

You could use a default value of None, as in:

class Queue:
  def __init__(self, max_size=None):
    self._qhead = None
    self._qtail = None
    self._count = 0
    self.max_size = max_size

and in the relevant methods:

if max_size is None:
    # code for the unbounded case
else:
    # we have a value for max_size, act accordingly
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50