Skip to main content
 首页 » 编程设计

python之类型错误 : Python thinks that I passed a function 2 arguments but I only passed it 1

2025年05月04日9daizhj

我在 Seattle Repy 中做某事这是 Python 的一个受限子集。无论如何,我想实现我自己的派生自 list 的 Queue:

class Queue(list): 
    job_count = 0 
 
    def __init__(self): 
        list.__init__(self) 
 
    def appendleft(item): 
        item.creation_time = getruntime() 
        item.current_count = self.job_count 
        self.insert(0, item) 
 
    def pop(): 
        item = self.pop() 
        item.pop_time = getruntime() 
        return item 

现在我在我的主服务器中调用它,我在其中使用我自己的作业类将作业传递给队列:

mycontext['queue'] = Queue() 
# ... 
job = Job(str(ip), message) 
mycontext['queue'].appendleft(job) 

最后一行引发了以下异常:

Exception (with type 'exceptions.TypeError'): appendleft() takes exactly 1 argument (2 given)

我是 Python 的新手,所以谁能向我解释为什么当显然只有一个参数时它会认为我给了 appendleft() 两个参数?

请您参考如下方法:

您必须在每个函数定义中输入自引用:

def appendleft(self, item):