在Python中访问集合类属性
我在Python3.6中工作,有两个类,一个类用作另一个类的列表的容器,但是嵌套类并不从高阶类继承。基本上,这里是我所看到的内容的简化:在Python中访问集合类属性,python,python-3.x,class,attributes,containers,Python,Python 3.x,Class,Attributes,Containers,我在Python3.6中工作,有两个类,一个类用作另一个类的列表的容器,但是嵌套类并不从高阶类继承。基本上,这里是我所看到的内容的简化: class Library(): def __init__(self, name, bookList): """ Initializes a library class object Send: self (Library object), name (str), list of books in th
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()-但是书籍不是库的子类,库只是包含书籍
- 在每个图书对象上维护库特征-但这看起来很笨重,会在整个库和图书对象中复制数据
我确信我遗漏了一些明显的东西,但我搜索的所有东西似乎都返回了关于继承的建议,这对我来说似乎没有意义。谢谢你的帮助 添加到
书中。\uuuu init\uuuu
:
self.library = None
for book in self.bookList:
book.library = self
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
添加到owningLibrary
:
if self.library is None:
return "No Library"
return self.library.name
添加到库。\uuuuu init\uuuuu
:
self.library = None
for book in self.bookList:
book.library = self
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
如果
书
没有一个属性告诉它,它就无法知道它在库
中。然后,库实例需要告诉所有的图书库包含它们。因为库和书是“定义”独立的,所以必须从书中显式地创建对库的引用
一种解决方案是将library
字段添加到Book
类中,该类将存储库的名称(或对library
对象的引用,取决于您计划进一步执行的操作)。这可以在库中完成
\uuuuu init\uuuuu
:
self.library = None
for book in self.bookList:
book.library = self
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
如果一本书
可能同时存在于多个库中:
- 初始化
self.library=[]
而不是self.library=None
李>
- 在
Library
中,构造函数执行book.libraries.append(self.name)
而不是book.Library=self.name
我这样做的方式是:
类库:
定义初始化(self,name):
self.name=name
self.books=[]
教材:
定义初始(自我、标题、作者、年份、库):
self.title=标题
self.author=作者
self.year=年
self.library=library
self.library.books.append(self)#把这本书放进图书馆
#创建库
orangeCountyLibrary=图书馆(“Orange县公共图书馆”
#创作书籍
战争与和平=书(“战争与和平”,“托尔斯泰,利奥”,
1869年,橘子园(图书馆)
《银河系搭便车指南》=Book(“银河系搭便车指南”,
“亚当斯,道格拉斯”,1985年,橘子国家图书馆)
#打印《搭便车指南》的当前所有者
印刷(搭便车指南.图书馆)#奥兰治县公共图书馆
这是tkinter处理将画布实例连接到Tk实例的方式。将书本添加到库中时,在书本上设置一个属性,该属性引用它所属的库?kindof perbook.SetLibrary(lib)和\uuu我的图书馆
在书
拥有的图书馆
里面似乎不是书
应该有的方法。