list - 名为 list 的内置类有什么作用?
问题描述
我正在阅读一本面向对象编程的书,并且正在阅读继承章节。有一个我不明白的示例代码。一个类继承了一个名为 list 的类,但它没有被定义。所以我通过在我的编辑器上输入 list 进行检查,它是蓝色的。所以这意味着它是一个类。我尝试在 Bing 上搜索以查看该课程做了什么,但我只获得有关 python 列表的文档和教程。这是代码:
class ContactList(list):
def search(self, name):
'''Return all contacts that contain the search value in their name.'''
matching_contacts = []
for contact in self:
if name in contact.name:
matching_contacts.append(contact)
return matching_contacts
class Contact:
all_contacts = ContactList()
def __init__(self, name, email):
self.name = name
self.email = email
Contact.all_contacts.append(self)
print(self.all_contacts)
class Supplier(Contact):
def order(self, order):
print("If this were a real system we would send "
"'{}' order to '{}'".format(order, self.name))
谢谢!
解决方案
推荐阅读
- .net - 您如何在 GraphQL 授权中记录安全性?
- firebase - 优化数据模型 Firestore
- sql - 将特定数据值拉入一行的最佳方法是什么?
- yii2 - 2 一个控制器中的删除功能 yii2
- reactjs - ESLint 必须使用解构状态赋值
- python - Compare dataframe cell value with series then find sum
- sql-server - 有人可以逐步指导我解决这个问题吗?对称对问题
- c# - 检索项目的父项时出错:找不到与给定名称“ThemeOverlay.AppCompat.Light”匹配的资源
- html - Scale Div with screen size including children, text and images
- python - 未加载库:@rpath/libboost_thread.dylib