首页 > 解决方案 > 名为 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))

谢谢!

标签: listclassooppython-3.7

解决方案


推荐阅读