Python (class,def,self)AttributeError:'xx'对象没有属性'xx'

Python (class,def,self)AttributeError:'xx'对象没有属性'xx',python,python-3.x,compiler-errors,Python,Python 3.x,Compiler Errors,我犯了这样的错误: 回溯最近一次呼叫上次: 文件xx,第51行,在 康特罗尔支票 文件xx,第46行,勾号 如果self.isSigned==True: AttributeError:“Sicherheit”对象没有属性“isSigned” 你能帮我吗 import hashlib class Sicherheit: passwordFile = 'usercreds.tmp' def Signup(self): self.isSigned = False # !

我犯了这样的错误:

回溯最近一次呼叫上次:

文件xx,第51行,在

康特罗尔支票

文件xx,第46行,勾号

如果self.isSigned==True:

AttributeError:“Sicherheit”对象没有属性“isSigned”

你能帮我吗

import hashlib
class Sicherheit:
    passwordFile = 'usercreds.tmp'
    def Signup(self):
        self.isSigned = False # !!! self.isSigned
        print("Sie müssen sich erst anmelden!\n")
        usernameInput = input("Bitte geben Sie Ihren Nutzername ein: \n")
        passwordInput = input("Bitte geben Sie Ihr Passwort ein: \n")
        usernameInputHashed = hashlib.sha512(usernameInput.encode())
        passwordInputHashed = hashlib.sha512(passwordInput.encode())

        with open(self.passwordFile, 'w') as f:
            f.write(str(usernameInputHashed.hexdigest()))
            f.write('\n')
            f.write(str(passwordInputHashed.hexdigest()))
            f.close()

        self.isSigned = True  # !!! self.isSigned
        print("Anmeldung war erfolgreich!\n")
        print("======================================================\n")
        self.Login()  # Moves onto the login def

    def Login(self):
        print("Sie müssen sich einloggen!\n")

        usernameEntry = input("Bitte geben Sie Ihren Nutzername ein: \n")
        passwordEntry = input("Bitte geben Sie Ihr Passwort ein: \n")
        usernameEntry = hashlib.sha512(usernameEntry.encode())
        passwordEntry = hashlib.sha512(passwordEntry.encode())
        usernameEntryHashed = usernameEntry.hexdigest()
        passwordEntryHashed = passwordEntry.hexdigest()

        with open(self.passwordFile) as r:
            info = r.readlines()
            usernameInFile = info[0].rstrip()
            passwordInFile = info[1].rstrip()

        if usernameEntryHashed == usernameInFile and passwordEntryHashed == passwordInFile:
            print("Anmeldung war erfolgreich!\n")

        else:
            print("Anmeldung war nicht erfolgreich!!!\n")
            self.Login()

    def CheckSign(self):
        if self.isSigned == True:  # !!! self.isSigned
            self.Login()
        else:
            self.Signup()
Kontrolle = Sicherheit()
Kontrolle.CheckSign()
移动线路

self.isSigned = False # !!! self.isSigned
从注册方法中取出并放入类变量中,或者为类创建一个uu init uu方法并在其中初始化它

当你打电话时:

Kontrolle = Sicherheit()
Kontrolle.CheckSign()
设置变量self.isSigned的代码从未执行过,它是注册方法的一部分,因此在调用:

Kontrolle = Sicherheit()
Kontrolle.CheckSign()
它查找尚未设置的变量,然后抛出错误:

AttributeError: 'Sicherheit' object has no attribute 'isSigned'
下面是如何在类中声明它:

class Sicherheit:
    passwordFile = 'usercreds.tmp'

    def __init__(self):
        self.isSigned = False

    def SignUp():
        ....

    ....

我如何在那里初始化它?