简体   繁体   中英

Why do I need a value for self here?

Hey there I'm new to python and try to program a simple login module which salts a password

When I use this class I get the following error:

TypeError: salting() missing 1 required positional argument: 'password'

class Login():
    def salting(self, username, password):
        self.password = password
        self.username = username

        print(self.username + self.password)

Login.salting("user1","pw1")

My Only Solutions were to use Login.salting("","user1","pw1") with an empty string for self or calling self as username end reuse it like this, but I think that I ran in an error, can someone help me:D

But when I compare that with my previous code which was like this (I learned that with this code) - the error doesn't appear...

class car():
    name = "BMW"
    color = "red"
    def redesign(self, color):
        self.color = color
c = car()
print(c.name)
print(c.color)
c.redesign("blue")
print(c.color)

THANKS

salting is an object method, not a class method. Each object has its own username and password attributes. You need to create a Login object, and then call the method on that.

s = Login()
s.salting("user1", "pw2")

This is analogous to using c = car() in the second block of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM