【问题标题】:(class,def,self) AttributeError: 'xx' object has no attribute 'xx'(class,def,self) AttributeError: 'xx' 对象没有属性 'xx'
【发布时间】:2017-10-28 21:26:02
【问题描述】:

我变成了这个错误:

Traceback(最近一次调用最后一次):

文件“xx”,第 51 行,在

Kontrolle.CheckSign()

CheckSign 中的文件“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()

【问题讨论】:

    标签: python python-3.x compiler-errors


    【解决方案1】:

    移动线

    self.isSigned = False # !!! self.isSigned
    

    从您的 SignUp 方法到您的类变量中,或者为您的类创建一个 __init__ 方法并在那里初始化它

    当你打电话时:

    Kontrolle = Sicherheit()
    

    设置变量self.isSigned 的代码永远不会执行(它是SignUp 方法的一部分,并且不会执行)所以当您调用时:

    Kontrolle.CheckSign()
    

    它会查找尚未设置的变量,然后抛出错误:

    AttributeError: 'Sicherheit' object has no attribute 'isSigned'
    

    在类中声明它的方式如下:

    class Sicherheit:
        passwordFile = 'usercreds.tmp'
    
        def __init__(self):
            self.isSigned = False
    
        def SignUp():
            ....
    
        ....
    

    【讨论】:

    • 我怎样才能在那里初始化它?
    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 2016-11-18
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    相关资源
    最近更新 更多