英文:
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:
- initialization of a base class (LibraryItem) in
__init__
ofBook
, which should be done sligthly differently I believe (see question for details) - 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论