2

I'm an experienced programmer, but completely new to Python. I've resolved most difficulties, but I can't get the queue module to work. Any help gratefully received. Python 3.2.

Reduced to its basic minimum, here's the issue:

>>>import queue
>>>q = queue.Queue
>>>q.qsize()
Traceback:
...
   q.qsize()
...
TypeError: qsize() takes 1 argument exactly (0 given)

Documentation...

7.8.1. Queue Objects

Queue objects (Queue, LifoQueue, or PriorityQueue) provide the public methods described below.

Queue.qsize()


OK - what argument.... ?

4

2 回答 2

5

您没有初始化实例,只是将类名重新分配给q. 它所谈论的“论据”是所有 Python 方法都需要self显式自引用。换句话说,它是说您正在尝试调用没有实例的实例方法。

>>> q = queue.Queue()
>>> q.qsize()

如果您从未见过 Python 方法定义,它看起来像这样:

class Queue(object):
    # Note the explicit 'self' argument
    def qsize(self):
        # ...
于 2013-07-24T15:47:15.920 回答
1

您只是重命名queue.Queue而不是实例化对象。

尝试这个

q = queue.Queue()
print q.qsize()
于 2013-07-24T15:49:30.387 回答