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('
')
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 个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:正好需要 3 个参数(给定 4 个)
基础教程推荐
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
