在Python中访问集合类属性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Python中访问集合类属性相关的知识,希望对你有一定的参考价值。
我在Python 3.6中工作并有两个类,一个类充当另一个类的容器,但嵌套类不从高阶类继承。基本上这里是我正在看的简化:
class Library():
def __init__(self, name, bookList):
"""
Initializes a library class object
Send: self (Library object), name (str), list of books in the library (list of Book objects)
"""
self.name=name
self.bookList=bookList
class Book():
def __init__(self, title, author, year):
"""
Initializes a book class object
Send: self (Book object), title (str), author (str), year (int)
"""
self.title=title
self.author=author
self.year=year
def owningLibrary(self):
"""
Identifies the name of the library that owns the book
Send: self (Book object)
"""
#some code that looks at the library's name and returns it
if __name__=="__main__":
#Create book
warAndPeace = Book("War and Peace", "Tolstoy, Leo", 1869)
hitchhikersGuide = Book("Hitchhiker's Guide to the Galaxy, The", "Adams, Douglas", 1985)
#Create library
orangeCountyLibrary = Library("Orange County Public Library", [warAndPeace, hitchhikersGuide])
#Print the current owner of Hitchhiker's Guide
print(hitchhikersGuide.owningLibrary())
我的问题是:如何启用包含的对象(书)来访问容器对象(库)的属性/方法。在我的示例中:返回拥有库的“name”变量
我考虑过的尝试:
- 继承+ super() - 但书籍不是库的子类,库只包含书籍
- 在每个书籍对象上维护库特征 - 但这看起来很笨重,并且在库和书籍对象之间复制数据
我确信有一些显而易见的东西让我失踪了,但我搜索过的所有东西似乎都回来了,对我来说似乎没有意义的继承建议。谢谢你的帮助!
答案
添加到Book.__init__
:
self.library = None
添加到owningLibrary
:
if self.library is None:
return "No Library"
return self.library.name
添加到Library.__init__
:
for book in self.bookList:
book.library = self
如果Book
没有具有告诉它的属性,就没有办法知道Library
是什么。然后,Library实例需要告诉所有书籍哪些库包含它们。
另一答案
由于Library
和Book
是“定义明智的”独立的,你必须明确地从library
创建一个book
的引用。
一种解决方案可能是将library
字段添加到Book
类中,该类将存储库的名称(或对Library
对象的引用,取决于您计划进一步做什么)。这可以在Library
__init__
中完成:
class Library:
def __init__(self, name, book_list):
"""
Initializes a library class object
Send: self (Library object), name (str), list of books in the library (list of Book objects)
"""
self.name = name
self.bookList = book_list
# set reference to the name of this library for every added book
for book in book_list:
book.library = self.name
class Book:
def __init__(self, title, author, year):
"""
Initializes a book class object
Send: self (Book object), title (str), author (str), year (int)
"""
self.title = title
self.author = author
self.year = year
self.library = None # book does not belong to any library yet
if __name__ == "__main__":
# Create book
warAndPeace = Book("War and Peace", "Tolstoy, Leo", 1869)
hitchhikersGuide = Book("Hitchhiker's Guide to the Galaxy, The", "Adams, Douglas", 1985)
# Create library
orangeCountyLibrary = Library("Orange County Public Library", [warAndPeace, hitchhikersGuide])
# Print the current owner of Hitchhiker's Guide
print(hitchhikersGuide.library) # Orange County Public Library
如果Book
可能同时在多个库中:
- 初始化
self.libraries = []
而不是self.library = None
; - 在
Library
构造函数做book.libraries.append(self.name)
而不是book.library = self.name
。
另一答案
我这样做的方式是:
class Library:
def __init__(self, name):
self.name=name
self.books=[]
class Book:
def __init__(self, title, author, year, library):
self.title=title
self.author=author
self.year=year
self.library=library
self.library.books.append(self)#put this book in the library
# Create library
orangeCountyLibrary = Library("Orange County Public Library"
# Create book
warAndPeace = Book("War and Peace", "Tolstoy, Leo",
1869,orangeCountyLibrary)
hitchhikersGuide = Book("Hitchhiker's Guide to the Galaxy, The",
"Adams, Douglas", 1985,orangeCountyLibrary)
# Print the current owner of Hitchhiker's Guide
print(hitchhikersGuide.library) # Orange County Public Library
this is the way tkinter handles connecting Canvas instances to Tk instances.
以上是关于在Python中访问集合类属性的主要内容,如果未能解决你的问题,请参考以下文章
在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途