简体   繁体   English

为什么我在这里需要一个自我价值?

[英]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嘿,我是 python 的新手,并尝试编写一个简单的登录模块来加盐密码

When I use this class I get the following error:当我使用这个 class 时,我收到以下错误:

TypeError: salting() missing 1 required positional argument: 'password'类型错误:salting() 缺少 1 个必需的位置参数:“密码”

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我唯一的解决方案是使用 Login.salting("","user1","pw1") 和一个空字符串作为 self 或调用 self 作为用户名 end 像这样重用它,但我认为我遇到了一个错误,有人可以帮帮我: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.加盐是一种salting方法,而不是 class 方法。 Each object has its own username and password attributes.每个 object 都有自己的usernamepassword属性。 You need to create a Login object, and then call the method on that.您需要创建一个Login object,然后调用该方法。

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

This is analogous to using c = car() in the second block of code.这类似于在第二个代码块中使用c = car()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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