Takes exactly 3 arguments (4 given)

I am refactoring the code to add object orientation and just checking 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 

When creating an instance of the class below, I get an odd error. That getip () takes exactly 3 arguments (4 data), which I don’t know how to solve.

 for theline in f: if "Failed password" in theline: lineclass = Lineobject(pattern, theline) else: pass 
+1
source share
2 answers

You provide self.getip() four arguments, because Python automatically adds self to the first argument for related methods. Expression:

 self.getip(self, pattern, line) 

leads to:

 getip(self, self, pattern, line) 

which is four arguments.

Do not go into self again:

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

The process of finding a method in an instance (via self.getip ) itself binds the method to handle this first argument for you.

+7
source

When you call the instance method, you are not explicitly passing the instance

t

 self.ip = self.getip(pattern, line) 
+2
source

Source: https://habr.com/ru/post/1273530/


All Articles