对 python 中的 size() 函数感到困惑

Confused about the size() function in python

我正在通过代码在 python

中编写一个循环队列

class CircularQueue:

  # constructor for the class
  # taking input for the size of the Circular queue 
  # from user
  def __init__(self, maxSize):
    self.queue = list()
    # user input value for maxSize
    self.maxSize = maxSize
    self.head = 0
    self.tail = 0

  # add element to the queue
  def enqueue(self, data):
    # if queue is full
    if self.size() == (self.maxSize - 1):
      return("Queue is full!")
    else:
      # add element to the queue
      self.queue.append(data)
      # increment the tail pointer
      self.tail = (self.tail+1) % self.maxSize
      return True

让我感到困惑的部分是方法 "enqueue"

中的 self.size()

我查看了 python 文档,没有看到任何 size() 函数,只看到 numpy 中对 size() 的引用。

通常你想调用 len() 来获取列表的大小,但我知道你不能这样做 self.len()

任何 clarity/explanation 编写此类内容背后的语法和逻辑都会有所帮助!

您需要定义自己的 size() 方法,并且只需 return 队列中当前持有的项目数。