正好接受3个参数(给定4个) [英] Takes exactly 3 arguments (4 given)
本文介绍了正好接受3个参数(给定4个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在重构代码以添加面向对象的功能,并且只是在测试代码.
i'm refactoring code in order to add object orientation and am just testing the code.
pattern = r"((([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])[ (\[]?(\.|dot)[ )\]]?){3}([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))"
class Lineobject(object):
def __init__(self, pattern, line):
self.ip = self.getip(self, pattern, line)
def getip (self, pattern, line):
for match in re.findall(pattern, line):
results = ''
ips = match[0]
usergeneratedblacklist.write(ips)
usergeneratedblacklist.write('\n')
return ips
实例化下面的类时,出现一个奇怪的错误. getip()的参数正好有3个参数(给定4个),我不知道该怎么解决.
When instantiating the class below I am getting an odd error. That of getip() takes exactly 3 arguments (4 given) which i do not know how to resolve.
for theline in f:
if "Failed password" in theline:
lineclass = Lineobject(pattern, theline)
else:
pass
推荐答案
您要给self.getip()
四个参数,因为Python会自动为绑定方法添加第一个self
参数.表达式:
You are giving self.getip()
four arguments because Python automatically adds in first self
argument for bound methods. The expression:
self.getip(self, pattern, line)
导致:
getip(self, self, pattern, line)
这是四个参数.
不再通过self
:
self.ip = self.getip(pattern, line)
(通过self.getip
)在实例上查找方法的行为绑定为您处理第一个参数的方法.
The very act of looking up the method on the instance (via self.getip
) binds the method to handle that first argument for you.
这篇关于正好接受3个参数(给定4个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文