slh*_*hck 1 python parameters arguments exception
我在Seattle Repy工作,这是Python的一个受限制的子集.无论如何,我想实现我自己的队列,它来自list:
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
Run Code Online (Sandbox Code Playgroud)
现在我在我的主服务器中调用它,在那里我使用自己的Job类将Jobs传递给Queue:
mycontext['queue'] = Queue()
# ...
job = Job(str(ip), message)
mycontext['queue'].appendleft(job)
Run Code Online (Sandbox Code Playgroud)
最后一行引发以下异常:
异常(类型为'exceptions.TypeError'):appendleft()只需1个参数(给定2个)
我对Python比较陌生,所以任何人都可以向我解释为什么appendleft()当显然只有一个时我会给出两个参数?
Python自动传递SELF(即当前对象)作为第一个参数,因此您需要将appendleft的函数定义更改为:
def appendleft(self, item):
Run Code Online (Sandbox Code Playgroud)
对于类中的其他函数定义也是如此.它们都需要SELF作为函数定义中的第一个参数,因此:
def pop():
Run Code Online (Sandbox Code Playgroud)
需要是:
def pop(self):
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1667 次 |
| 最近记录: |