Py学习  »  Python

具有可选绑定的python链表队列

buchelle • 4 年前 • 630 次点击  

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

但我需要允许构造函数接受一个可选参数,该参数指示队列的最大大小,如果没有给定大小,则队列是无限的。

我该怎么做?短暂性脑缺血发作

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54954
 
630 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Thierry Lathuille
Reply   •   1 楼
Thierry Lathuille    4 年前

可以使用默认值 None

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

在相关方法中:

if max_size is None:
    # code for the unbounded case
else:
    # we have a value for max_size, act accordingly
Diego
Reply   •   2 楼
Diego    4 年前
class Queue:
  def __init__(self, size=Value):
    self._qhead = None
    self._qtail = None
    self._count = 0

您可以设置一个默认值,将参数设置为您想要的值。如果用另一个参数创建一个对象,它将替换默认值。