如何解决 AttributeError: 类型对象 ‘LibraryItem’ 没有属性 ‘Book_Title’?

huangapple go评论87阅读模式
英文:

How to solve AttributeError: type object 'LibraryItem' has no attribute 'Book_Title'?

问题

在运行这段代码后发生错误。它说"AttributeError: type object 'LibraryItem' has no attribute 'Book_Title"。在派生类中是否可以从基类中访问定义的属性,比如从方法中访问它?

import datetime

class LibraryItem:
    def __init__(self, t, a, i):
        self.__Title = t
        self.__Author = a
        self.__ItemID = i
        self.__OnLoan = False
        self.__DueDate = datetime.date.today()

    def SetOnLoan(self, o):
        self.__OnLoan = o

    def SetDueDate(self, d):
        self.__DueDate = d

class Book(LibraryItem):
    def __init__(self, t, a, i):
        LibraryItem.__init__(self, t, a, i)
        self.__RequestedBy = 'people'

    def SetRequestedBy(self, r):
        self.__RequestedBy = r

    def GetRequestedBy(self):
        return self.__RequestedBy

    def PrintDetails(self):
        print(LibraryItem.__Title)

ThisBook = Book('Python', 'Tom', '123')
ThisBook.PrintDetails()

错误发生在这行代码上:

print(LibraryItem.__Title)

应该修改为:

print(self._LibraryItem__Title)
英文:

Error occurs after running this piece of code. It says "AttributeError: type object 'LibraryItem' has no attribute 'Book_Title". Can an attribute defined in base class be accessed from derived class such as accessing it from a method?

import datetime

class LibraryItem:
    def __init__(self,t,a,i):
        self.__Title = t
        self.__Author = a
        self.__ItemID = i
        self.__OnLoan = False
        self.__DuteDate = datetime.date.today()

    def SetOnLoan(self,o):
        self.__OnLoan = o
    def SetDueDate(self,d):
        self.__DueDate = d

class Book(LibraryItem):
    def __init__(self,t,a,i):
        LibraryItem.__init__(self,t,a,i)
        self.__RequestedBy = 'people'

    def SetRequestedBy(self,r):
        self.__RequestedBy = r

    def GetRequestedBy(self):
        return self.__RequestedBy

    def PrintDetails(self):
        print(LibraryItem.__Title)

ThisBook = Book('Python','Tom','123')
ThisBook.PrintDetails()

答案1

得分: 1

有两个要点:
1)在Book__init__方法中对基类(LibraryItem)进行初始化,我认为应该稍微有所不同(详见问题以获取详细信息)。
2)类属性的私有性是通过不同数量的下划线来实现的(详见问题以获取详细信息)。基本上,两个下划线隐藏了属性。

以下是代码部分,不需要翻译:

import datetime

class LibraryItem:
    def __init__(self,t,a,i):
        self.__Title = t
        self.__Author = a
        self.__ItemID = i
        self.__OnLoan = False
        self.__DueDate = datetime.date.today()

    def SetOnLoan(self,o):
        self.__OnLoan = o
    def SetDueDate(self,d):
        self.__DueDate = d
    
    def GetTitle(self):
        return self.__Title

class Book(LibraryItem):
    def __init__(self,t,a,i):
        super(Book, self).__init__(t,a,i)
        self.__RequestedBy = 'people'

    def SetRequestedBy(self,r):
        self.__RequestedBy = r

    def GetRequestedBy(self):
        return self.__RequestedBy

    def PrintDetails(self):
        print(self.GetTitle())

ThisBook = Book('Python','Tom','123')
ThisBook.PrintDetails()

产生的输出:

Python
英文:

There're 2 points here:

  1. initialization of a base class (LibraryItem) in __init__ of Book, which should be done sligthly differently I believe (see question for details)
  2. the privacy of class attributes, which is achieved by different number of underscores (see question for details). Basically two undescores hide the attribute.

The code

import datetime

class LibraryItem:
    def __init__(self,t,a,i):
        self.__Title = t
        self.__Author = a
        self.__ItemID = i
        self.__OnLoan = False
        self.__DuteDate = datetime.date.today()

    def SetOnLoan(self,o):
        self.__OnLoan = o
    def SetDueDate(self,d):
        self.__DueDate = d
    
    def GetTitle(self):
        return self.__Title

class Book(LibraryItem):
    def __init__(self,t,a,i):
        super(Book, self).__init__(t,a,i)
        self.__RequestedBy = 'people'

    def SetRequestedBy(self,r):
        self.__RequestedBy = r

    def GetRequestedBy(self):
        return self.__RequestedBy

    def PrintDetails(self):
        print(self.GetTitle())

ThisBook = Book('Python','Tom','123')
ThisBook.PrintDetails()

Produces output:

Python

huangapple
  • 本文由 发表于 2023年7月12日 23:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定