python - (class,def,self)AttributeError : 'xx' object has no attribute 'xx'

标签 python python-3.x compiler-errors

我变成这个错误:

追溯(最近一次通话):

文件“xx”,第51行,在

Kontrolle.CheckSign()



在CheckSign中的文件“xx”,第46行

if 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

退出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():
        ....

    ....

关于python - (class,def,self)AttributeError : 'xx' object has no attribute 'xx' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989736/

相关文章:

python - json.loads() 给出异常,它期望一个值,看起来值在那里

python-3.x - 无限实时连续流式传输音频信号,Python

python argparse : allow unregistered arguments

Python-Matplotlib : normalize axis when plotting a Probability Density Function

python - 如何将多个装饰器打包成一个?

AndroidManifest.xml - 当我构建项目时,一行会自动更改

c - 在 linux 帐户中运行 C 但在 mac 终端上运行 C 时出现错误消息

c++ - C++程序可以在if/else语句中包含switch语句吗?

python - 如何在 Tkinter 中使用 Canvas 绘制点?

python - 当值通过循环进入时,字典 0 键值未被拾取