T3CHNOCIDE
T3CHNOCIDE

Reputation: 43

Python 2.7 TypeError: __init__() takes exactly 4 arguments (1 given)

After looking through all the other questions of this nature the answer seems to be the same, that the coder is not passing the arguments because they've left __init__() blank. However, in the code below I have not left it blank and have tried to pass all four arguments as seen in the last line.

Yet I'm still getting the TypeError. I can't figure out why the arguments I am trying to pass are being ignored.

class Login:
    def __init__(self, username, password, site):
        self.username = username
        self.password = password
        self.site = "http://" + site + ".wikia.com"
        Login.login(self)

    def login(self):
        self.session = requests.session()
        login_data = {'action': 'login', 'lgname': self.username, 'lgpassword': self.password, 'format':'json'}
        response = self.session.post(self.site + "/api.php",data=login_data)
        content = json.loads(response.content)
        login_data['lgtoken'] = content['login']['token']
        response = self.session.post(self.site + "/api.php",data=login_data)
        content = json.loads(response.content)
        if content['login']['result'] != 'Success':
            raise Exception("Login error: Could not log in via MediaWiki API.")

if __name__ == '__main__':
    Login = Login()
    Login.__init__(self, "username", "password", "site")

Any help is appreciated. Thanks!

Upvotes: 1

Views: 3652

Answers (2)

MattDMo
MattDMo

Reputation: 102852

While mu 無 shows the correct way to do it, I wanted to emphasize something in his code that fixes another issue in yours. Your code is:

if __name__ == '__main__':
    Login = Login()
    Login.__init__(self, "username", "password", "site")

Even if you instantiated Login() correctly by running

Login = Login("username", "password", "site")

you wouldn't be able to use your Login class again, because you just overwrote it with your Login variable. Make sure your instance is not the same as the class name, use

login = Login("username", "password", "site")

instead.

Upvotes: 8

Anshul Goyal
Anshul Goyal

Reputation: 76887

You are calling the __init__ incorrectly, use this instead. __init__ is called when you are initialising the new object automatically:

if __name__ == '__main__':
    login = Login("username", "password", "site")

You should check python docs about the same as well.

Upvotes: 2

Related Questions