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

时间:2017-10-28 12:28:44

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

我成了这个错误:

追踪(最近一次呼叫最后一次):

文件“xx”,第51行,

  

Kontrolle.CheckSign()

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

  

如果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()

1 个答案:

答案 0 :(得分:1)

移动线

def self.scrape_details_page(library_url)
    details_page = Nokogiri::HTML(open(library_url))

    library_name = details_page.css("h3")

    details_page.css("table tr").collect do |level|
        case level.css("a[href]").text.downcase
            when "level 1"
                name = level.css("a[href]").text
                total_available = level.css(".right").text.split(" ")[0]
                out_of_available = level.css(".right").text.split(" ")[3]
                level = {name: name, total_available: total_available, out_of_available: out_of_available}
            when "level 2"
                name = level.css("a[href]").text
                total_available = level.css(".right").text.split(" ")[0]
                out_of_available = level.css(".right").text.split(" ")[3]
                level = {name: name, total_available: total_available, out_of_available: out_of_available}
         end
     end
end

从您的self.isSigned = False # !!! self.isSigned 方法进入您的类变量,或者为您的类创建SignUp方法并在其中初始化

致电时:

__init__

设置变量Kontrolle = Sicherheit() 的代码永远不会被执行(它是self.isSigned方法的一部分而且没有被执行)所以当你打电话时:

SignUp

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

Kontrolle.CheckSign()

以下是您在班级中声明的方式:

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