Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

python - Takes exactly 3 arguments (4 given)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

results in:

getip(self, self, pattern, line)

which is four arguments.

Don't pass in self again:

self.ip = self.getip(pattern, line)

The very act of looking up the method on the instance (via self.getip) binds the method to handle that first argument for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...