Hướng dẫn what class is a list in python? - lớp nào là một danh sách trong python?

Xem thảo luận

Show

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcinstances to list. By this, every index in the list can point to instance attributes and methods of the class and can access them. If you observe it closely, a list of objects behaves like an array of structures in C. Let’s try to understand it better with the help of examples.

    Bàn luận

    Chúng ta có thể tạo danh sách đối tượng trong Python bằng cách nối thêm các phiên bản lớp vào danh sách. Bằng cách này, mọi chỉ mục trong danh sách đều có thể trỏ đến các thuộc tính và phương thức của lớp và có thể truy cập chúng. Nếu bạn quan sát nó một cách chặt chẽ, một danh sách các đối tượng hoạt động giống như một mảng các cấu trúc trong C. Hãy để cố gắng hiểu nó tốt hơn với sự trợ giúp của các ví dụ.

    Ví dụ 1:

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    4
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    5

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    6
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    7
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    8
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    0

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    1
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    3
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    5

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    1
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    8
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    0

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    5
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    2
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    7
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    9

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    5
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    8
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    7
    # number_list.py
    
    class NumberList(list):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, self._validate_number(item))
    
        def insert(self, index, item):
            super().insert(index, self._validate_number(item))
    
        def append(self, item):
            super().append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    
    0
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    9

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    3

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    5
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    6
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    7
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    8
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    9

    Output:

    Akash 2
    Deependra 40
    Reaper 44
    

    # number_list.py
    
    class NumberList(list):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, self._validate_number(item))
    
        def insert(self, index, item):
            super().insert(index, self._validate_number(item))
    
        def append(self, item):
            super().append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    
    2
    # number_list.py
    
    class NumberList(list):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, self._validate_number(item))
    
        def insert(self, index, item):
            super().insert(index, self._validate_number(item))
    
        def append(self, item):
            super().append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    
    3
    # number_list.py
    
    class NumberList(list):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, self._validate_number(item))
    
        def insert(self, index, item):
            super().insert(index, self._validate_number(item))
    
        def append(self, item):
            super().append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    
    4
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # number_list.py
    
    class NumberList(list):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, self._validate_number(item))
    
        def insert(self, index, item):
            super().insert(index, self._validate_number(item))
    
        def append(self, item):
            super().append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    
    6

    Chúng ta có thể tạo danh sách đối tượng trong Python bằng cách nối thêm các phiên bản lớp vào danh sách. Bằng cách này, mọi chỉ mục trong danh sách đều có thể trỏ đến các thuộc tính và phương thức của lớp và có thể truy cập chúng. Nếu bạn quan sát nó một cách chặt chẽ, một danh sách các đối tượng hoạt động giống như một mảng các cấu trúc trong C. Hãy để cố gắng hiểu nó tốt hơn với sự trợ giúp của các ví dụ.

    Ví dụ 1:

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    4
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    5

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    6
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    7
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    8
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    0

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    1
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    3
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    5

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    1
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    8
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    0

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    1
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    3
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    5

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    5
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    8
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    7
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    01
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    9

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    5
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    05
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    7
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    07
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    9

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    5
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    11
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    7
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    13
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    9

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    4
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    3

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    6
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    21
    # custom_list.py
    
    class CustomList(list):
        def join(self, separator=" "):
            return separator.join(str(item) for item in self)
    
        def map(self, action):
            return type(self)(action(item) for item in self)
    
        def filter(self, predicate):
            return type(self)(item for item in self if predicate(item))
    
        def for_each(self, func):
            for item in self:
                func(item)
    
    2
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    23


    Tại một số điểm trong cuộc phiêu lưu mã hóa Python của bạn, bạn có thể cần tạo các lớp giống như danh sách tùy chỉnh với hành vi sửa đổi, chức năng mới hoặc cả hai. Để thực hiện điều này trong Python, bạn có thể kế thừa từ một lớp cơ sở trừu tượng, trực tiếp đến lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp hoặc kế thừa từ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, sống trong mô-đun
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26.custom list-like classes with modified behavior, new functionalities, or both. To do this in Python, you can inherit from an abstract base class, subclass the built-in
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 class directly, or inherit from
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, which lives in the
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26 module.

    Trong hướng dẫn này, bạn sẽ học cách:

    • Tạo các lớp giống như danh sách tùy chỉnh bằng cách kế thừa từ lớp
      # string_list.py
      
      from collections import UserList
      
      class StringList(UserList):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              self.data[index] = str(item)
      
          def insert(self, index, item):
              self.data.insert(index, str(item))
      
          def append(self, item):
              self.data.append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  self.data.extend(other)
              else:
                  self.data.extend(str(item) for item in other)
      
      1 tích hợpbuilt-in
      # string_list.py
      
      from collections import UserList
      
      class StringList(UserList):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              self.data[index] = str(item)
      
          def insert(self, index, item):
              self.data.insert(index, str(item))
      
          def append(self, item):
              self.data.append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  self.data.extend(other)
              else:
                  self.data.extend(str(item) for item in other)
      
      1 class
    • Xây dựng các lớp giống như danh sách tùy chỉnh bằng cách phân lớp
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      25 từ mô-đun
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      26
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      25
      from the
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      26 module

    Bạn cũng sẽ viết một số ví dụ về việc mà bạn sẽ giúp bạn quyết định lớp cha,

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 hoặc
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, để sử dụng khi tạo các lớp danh sách tùy chỉnh của bạn.

    Để tận dụng tối đa hướng dẫn này, bạn nên quen thuộc với lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp của Python và các tính năng tiêu chuẩn của nó. Bạn cũng cần biết những điều cơ bản của lập trình hướng đối tượng và hiểu cách thức kế thừa hoạt động trong Python.

    Tạo các lớp giống như danh sách trong Python

    Lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp là một loại dữ liệu cơ bản trong Python. Danh sách này rất hữu ích trong nhiều tình huống và có hàng tấn trường hợp sử dụng thực tế. Trong một số trường hợp sử dụng này, chức năng tiêu chuẩn của Python
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 có thể không đủ và bạn có thể cần phải tạo các lớp giống như danh sách tùy chỉnh để giải quyết vấn đề trong tay.

    Bạn thường tìm thấy ít nhất hai lý do để tạo các lớp giống như danh sách tùy chỉnh:

    1. Mở rộng danh sách thông thường bằng cách thêm chức năng mới the regular list by adding new functionality
    2. Sửa đổi chức năng danh sách tiêu chuẩn the standard list’s functionality

    Bạn cũng có thể đối mặt với các tình huống mà bạn cần để mở rộng và sửa đổi chức năng tiêu chuẩn của danh sách.

    Tùy thuộc vào nhu cầu cụ thể và cấp độ kỹ năng của bạn, bạn có thể sử dụng một vài chiến lược để tạo các lớp giống như danh sách tùy chỉnh của riêng bạn. Bạn có thể:

    • Kế thừa từ một lớp cơ sở trừu tượng thích hợp, chẳng hạn như
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      35
    • Kế thừa từ lớp Python tích hợp trực tiếp
      # string_list.py
      
      from collections import UserList
      
      class StringList(UserList):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              self.data[index] = str(item)
      
          def insert(self, index, item):
              self.data.insert(index, str(item))
      
          def append(self, item):
              self.data.append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  self.data.extend(other)
              else:
                  self.data.extend(str(item) for item in other)
      
      1
    • Lớp con
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      25 từ
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      26

    Có một vài cân nhắc khi bạn chọn chiến lược thích hợp để sử dụng. Hãy đọc để biết thêm chi tiết.

    Xây dựng một lớp giống như danh sách từ một lớp cơ sở trừu tượng

    Bạn có thể tạo các lớp giống như danh sách của riêng mình bằng cách kế thừa từ một lớp cơ sở trừu tượng thích hợp (ABC), như

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    35. ABC này cung cấp các triển khai chung của hầu hết các phương thức
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 ngoại trừ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    41,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    42,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    43,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    44 và
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    45. Vì vậy, khi kế thừa từ lớp này, bạn sẽ phải tự mình thực hiện các phương pháp này.abstract base class (ABC), like
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    35. This ABC provides generic implementations of most
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 methods except for
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    41,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    42,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    43,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    44, and
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    45. So, when inheriting from this class, you’ll have to implement these methods yourself.

    Viết triển khai của riêng bạn cho tất cả các phương pháp đặc biệt này là một lượng công việc hợp lý. Nó dễ bị lỗi và yêu cầu kiến ​​thức nâng cao về Python và mô hình dữ liệu của nó. Nó cũng có thể ngụ ý các vấn đề về hiệu suất bởi vì bạn sẽ viết các phương thức trong Python thuần túy.

    Ngoài ra, giả sử bạn cần tùy chỉnh chức năng của bất kỳ phương thức danh sách tiêu chuẩn nào khác, như

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    46 hoặc
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    45. Trong trường hợp đó, bạn sẽ phải ghi đè triển khai mặc định và cung cấp một triển khai phù hợp đáp ứng nhu cầu của bạn.

    Ưu điểm chính của chiến lược này để tạo các lớp giống như danh sách là lớp ABC cha mẹ sẽ cảnh báo bạn nếu bạn bỏ lỡ bất kỳ phương thức cần thiết nào trong triển khai tùy chỉnh của bạn.

    Nói chung, bạn chỉ nên nắm lấy chiến lược này nếu bạn cần một lớp giống như danh sách mà khác về cơ bản so với lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp.

    Trong hướng dẫn này, bạn sẽ tập trung vào việc tạo các lớp giống như danh sách bằng cách kế thừa từ lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp và lớp
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 từ mô-đun thư viện tiêu chuẩn
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26. Những chiến lược này dường như là những chiến lược nhanh nhất và thực tế nhất.

    Kế thừa từ lớp Python, tích hợp # string_list.py from collections import UserList class StringList(UserList): def __init__(self, iterable): super().__init__(str(item) for item in iterable) def __setitem__(self, index, item): self.data[index] = str(item) def insert(self, index, item): self.data.insert(index, str(item)) def append(self, item): self.data.append(str(item)) def extend(self, other): if isinstance(other, type(self)): self.data.extend(other) else: self.data.extend(str(item) for item in other) 1

    Trong một thời gian dài, không thể kế thừa trực tiếp từ các loại Python được thực hiện trong C. Python 2.2 đã khắc phục vấn đề này. Bây giờ bạn có thể phân lớp các loại tích hợp, bao gồm

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1. Thay đổi này đã mang lại một số lợi thế kỹ thuật cho các lớp con vì bây giờ họ:

    • Sẽ hoạt động ở mọi nơi yêu cầu loại tích hợp ban đầu
    • Có thể xác định các phương thức phiên bản mới, tĩnh và lớp
    • Có thể lưu trữ các thuộc tính thể hiện của chúng trong thuộc tính lớp
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      54, về cơ bản thay thế thuộc tính
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      55

    Mục đầu tiên trong danh sách này có thể là một yêu cầu đối với mã C mong đợi một lớp tích hợp Python. Mục thứ hai cho phép bạn thêm chức năng mới trên hành vi danh sách tiêu chuẩn. Cuối cùng, mục thứ ba sẽ cho phép bạn hạn chế các thuộc tính của một lớp con chỉ có các thuộc tính được xác định trước trong

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    54.

    Để khởi động mọi thứ và bắt đầu tạo các lớp giống như danh sách tùy chỉnh, giả sử rằng bạn cần một danh sách tự động lưu trữ tất cả các mục của nó dưới dạng chuỗi. Giả sử rằng danh sách tùy chỉnh của bạn sẽ chỉ lưu trữ các số làm chuỗi, bạn có thể tạo lớp con sau của

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1:

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    

    Các lớp con

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58 của bạn trực tiếp
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1, điều đó có nghĩa là nó sẽ kế thừa tất cả các chức năng của Python
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tiêu chuẩn. Vì bạn muốn danh sách của mình lưu trữ các mục dưới dạng chuỗi, bạn cần sửa đổi tất cả các phương thức thêm hoặc sửa đổi các mục trong danh sách cơ bản. Những phương pháp đó bao gồm những điều sau:

    • # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      61 Khởi tạo tất cả các phiên bản mới của lớp.
      initializes all the class’s new instances.
    • # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      42 cho phép bạn gán một giá trị mới cho một mục hiện tại bằng cách sử dụng chỉ mục của mục, như trong
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      63.
      allows you to assign a new value to an existing item using the item’s index, like in
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      63.
    • # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      45 cho phép bạn chèn một mục mới tại một vị trí nhất định trong danh sách cơ bản bằng cách sử dụng chỉ mục vật phẩm.
      allows you to insert a new item at a given position in the underlying list using the item’s index.
    • # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      46 thêm một mục mới duy nhất ở cuối danh sách cơ bản.
      adds a single new item at the end of the underlying list.
    • # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      66 thêm một loạt các mục vào cuối danh sách.
      adds a series of items to the end of the list.

    Các phương pháp khác mà lớp

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58 của bạn được kế thừa từ
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 hoạt động tốt vì họ không thêm hoặc cập nhật các mục trong danh sách tùy chỉnh của bạn.

    Để sử dụng

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58 trong mã của bạn, bạn có thể làm điều gì đó như sau:

    >>>

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    

    Lớp học của bạn hoạt động như mong đợi. Nó chuyển đổi tất cả các giá trị đầu vào thành các chuỗi khi đang bay. Điều đó thật tuyệt, phải không? Khi bạn tạo một thể hiện mới là

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58, trình khởi tạo lớp học sẽ chăm sóc chuyển đổi.

    Khi bạn nối, chèn, mở rộng hoặc gán các giá trị mới cho các phiên bản lớp, các phương thức hỗ trợ mỗi thao tác sẽ chăm sóc quy trình chuyển đổi chuỗi. Bằng cách này, danh sách của bạn sẽ luôn lưu trữ các mục của nó dưới dạng các đối tượng chuỗi.

    Phân lớp # string_list.py class StringList(list): def __init__(self, iterable): super().__init__(str(item) for item in iterable) def __setitem__(self, index, item): super().__setitem__(index, str(item)) def insert(self, index, item): super().insert(index, str(item)) def append(self, item): super().append(str(item)) def extend(self, other): if isinstance(other, type(self)): super().extend(other) else: super().extend(str(item) for item in other) 25 từ # string_list.py class StringList(list): def __init__(self, iterable): super().__init__(str(item) for item in iterable) def __setitem__(self, index, item): super().__setitem__(index, str(item)) def insert(self, index, item): super().insert(index, str(item)) def append(self, item): super().append(str(item)) def extend(self, other): if isinstance(other, type(self)): super().extend(other) else: super().extend(str(item) for item in other) 26

    Một cách khác để tạo một lớp giống như danh sách tùy chỉnh là sử dụng lớp

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 từ mô-đun
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26. Lớp này là một trình bao bọc xung quanh loại
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp. Nó được thiết kế để tạo lại các đối tượng giống như danh sách khi nó không thể kế thừa trực tiếp từ lớp
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp.

    Mặc dù nhu cầu về lớp này đã được thay thế một phần bởi khả năng phân lớp trực tiếp lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 vẫn có sẵn trong thư viện tiêu chuẩn, cả để thuận tiện và tương thích ngược.

    Tính năng phân biệt của

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 là nó cho phép bạn truy cập vào thuộc tính
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 của nó, có thể tạo điều kiện cho việc tạo danh sách tùy chỉnh của bạn vì bạn không cần phải sử dụng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81 mọi lúc. Thuộc tính
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 chứa một Python
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 thông thường, trống theo mặc định.

    Tại đây, cách bạn có thể thực hiện lại lớp

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58 của mình bằng cách kế thừa từ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25:

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    

    Trong ví dụ này, có quyền truy cập vào thuộc tính

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 cho phép bạn mã hóa lớp theo cách đơn giản hơn bằng cách sử dụng Phái đoàn, điều đó có nghĩa là danh sách trong
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 đảm nhận việc xử lý tất cả các yêu cầu.

    Bây giờ bạn gần như không phải sử dụng các công cụ nâng cao như

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81. Bạn chỉ cần gọi chức năng này trong trình khởi tạo lớp để ngăn chặn các vấn đề trong các kịch bản kế thừa hơn nữa. Trong phần còn lại của các phương pháp, bạn chỉ cần tận dụng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80, nắm giữ danh sách Python thông thường. Làm việc với danh sách là một kỹ năng mà bạn có thể đã có.

    Phiên bản mới này hoạt động giống như phiên bản đầu tiên của bạn là

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58. Hãy tiếp tục và chạy mã sau để dùng thử:

    >>>

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    

    Phơi bày

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 là tính năng phù hợp nhất của
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, như bạn đã học. Thuộc tính này có thể đơn giản hóa các lớp của bạn vì bạn không cần phải sử dụng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81 mọi lúc. Bạn chỉ có thể tận dụng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 và sử dụng giao diện
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 quen thuộc để hoạt động với thuộc tính này.

    Các lớp học giống như danh sách mã hóa: Các ví dụ thực tế

    Bạn đã biết cách sử dụng

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 và
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 khi bạn cần tạo các lớp giống như danh sách tùy chỉnh để thêm hoặc sửa đổi chức năng tiêu chuẩn của
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1.

    Phải thừa nhận rằng, khi bạn nghĩ đến việc tạo ra một lớp giống như danh sách, kế thừa từ

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 có lẽ có vẻ tự nhiên hơn là kế thừa từ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 vì các nhà phát triển Python biết về
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1. Họ có thể không nhận thức được sự tồn tại của
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25.

    Bạn cũng biết rằng sự khác biệt chính giữa hai lớp này là khi bạn kế thừa từ

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, bạn có quyền truy cập vào thuộc tính
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80, đây là danh sách thông thường mà bạn có thể thao tác qua giao diện
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tiêu chuẩn. Ngược lại, việc kế thừa từ
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 đòi hỏi kiến ​​thức nâng cao về mô hình dữ liệu Python, bao gồm các công cụ như hàm
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81 tích hợp và một số phương pháp đặc biệt.

    Trong các phần sau, bạn sẽ mã hóa một vài ví dụ thực tế bằng cả hai lớp. Sau khi viết các ví dụ này, bạn sẽ chuẩn bị tốt hơn để chọn công cụ phù hợp để sử dụng khi bạn cần xác định các lớp giống như danh sách tùy chỉnh trong mã của mình.

    Một danh sách chỉ chấp nhận dữ liệu số

    Như một ví dụ đầu tiên về việc tạo một lớp giống như danh sách với hành vi tùy chỉnh, hãy nói rằng bạn chỉ cần một danh sách chỉ chấp nhận dữ liệu số. Danh sách của bạn chỉ nên lưu trữ số nguyên, phao và số phức. Nếu bạn cố gắng lưu trữ giá trị của bất kỳ loại dữ liệu nào khác, như một chuỗi, thì danh sách của bạn sẽ tăng

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    08.

    Ở đây, một triển khai của một lớp

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    09 với chức năng mong muốn:

    # number_list.py
    
    class NumberList(list):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, self._validate_number(item))
    
        def insert(self, index, item):
            super().insert(index, self._validate_number(item))
    
        def append(self, item):
            super().append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    

    Trong ví dụ này, lớp

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    09 của bạn kế thừa trực tiếp từ
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1. Điều này có nghĩa là lớp của bạn chia sẻ tất cả các chức năng cốt lõi với lớp
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp. Bạn có thể lặp lại các trường hợp
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    09, truy cập và cập nhật các mục của nó bằng các chỉ số của họ, gọi các phương thức
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 chung và hơn thế nữa.

    Bây giờ, để đảm bảo rằng mọi mục đầu vào là một số, bạn cần xác thực từng mục trong tất cả các phương thức hỗ trợ các hoạt động để thêm các mục mới hoặc cập nhật các mục hiện có trong danh sách. Các phương pháp cần thiết giống như trong ví dụ

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58 trở lại trong phần kế thừa từ phần lớp
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp của Python.

    Để xác thực dữ liệu đầu vào, bạn sử dụng phương thức trợ giúp gọi là

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    17. Phương pháp này sử dụng hàm
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    18 tích hợp để kiểm tra xem giá trị đầu vào hiện tại có phải là một thể hiện là
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    19,
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    20 hoặc
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    21, là các lớp tích hợp đại diện cho các giá trị số trong Python.

    Nếu giá trị đầu vào là một thể hiện của kiểu dữ liệu số, thì hàm trợ giúp của bạn tự trả về chính giá trị. Mặt khác, chức năng làm tăng ngoại lệ

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    08 với thông báo lỗi thích hợp.

    Để sử dụng

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    09, hãy quay lại phiên tương tác của bạn và chạy mã sau:

    >>>

    >>> from number_list import NumberList
    
    >>> numbers = NumberList([1.1, 2, 3j])
    >>> numbers
    [1.1, 2, 3j]
    
    >>> numbers.append("4.2")
    Traceback (most recent call last):
        ...
    TypeError: numeric value expected, got str
    
    >>> numbers.append(4.2)
    >>> numbers
    [1.1, 2, 3j, 4.2]
    
    >>> numbers.insert(0, "0")
    Traceback (most recent call last):
        ...
    TypeError: numeric value expected, got str
    
    >>> numbers.insert(0, 0)
    >>> numbers
    [0, 1.1, 2, 3j, 4.2]
    
    >>> numbers.extend(["5.3", "6"])
    Traceback (most recent call last):
        ...
    TypeError: numeric value expected, got str
    
    >>> numbers.extend([5.3, 6])
    >>> numbers
    [0, 1.1, 2, 3j, 4.2, 5.3, 6]
    

    Phơi bày

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 là tính năng phù hợp nhất của
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, như bạn đã học. Thuộc tính này có thể đơn giản hóa các lớp của bạn vì bạn không cần phải sử dụng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81 mọi lúc. Bạn chỉ có thể tận dụng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 và sử dụng giao diện
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 quen thuộc để hoạt động với thuộc tính này.

    Các lớp học giống như danh sách mã hóa: Các ví dụ thực tế

    # number_list.py
    
    from collections import UserList
    
    class NumberList(UserList):
        def __init__(self, iterable):
            super().__init__(self._validate_number(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = self._validate_number(item)
    
        def insert(self, index, item):
            self.data.insert(index, self._validate_number(item))
    
        def append(self, item):
            self.data.append(self._validate_number(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(self._validate_number(item) for item in other)
    
        def _validate_number(self, value):
            if isinstance(value, (int, float, complex)):
                return value
            raise TypeError(
                f"numeric value expected, got {type(value).__name__}"
            )
    

    Bạn đã biết cách sử dụng

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 và
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 khi bạn cần tạo các lớp giống như danh sách tùy chỉnh để thêm hoặc sửa đổi chức năng tiêu chuẩn của
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1.

    Phải thừa nhận rằng, khi bạn nghĩ đến việc tạo ra một lớp giống như danh sách, kế thừa từ

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 có lẽ có vẻ tự nhiên hơn là kế thừa từ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 vì các nhà phát triển Python biết về
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1. Họ có thể không nhận thức được sự tồn tại của
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25.

    Lưu ý rằng bạn chỉ sử dụng

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81 trong trình khởi tạo lớp,
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    37. Đây là một thực tiễn tốt nhất khi bạn làm việc với kế thừa trong Python. Nó cho phép bạn khởi tạo đúng các thuộc tính trong lớp cha mà không phá vỡ mọi thứ.

    Một danh sách có chức năng bổ sung

    Bây giờ nói rằng bạn cần một lớp giống như danh sách với tất cả các chức năng tiêu chuẩn của Python

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 thông thường. Lớp học của bạn cũng sẽ cung cấp một số chức năng bổ sung được mượn từ kiểu dữ liệu mảng của JavaScript. Ví dụ: bạn sẽ cần phải có các phương thức như sau:

    • >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      39 kết hợp tất cả các mục trong danh sách trong một chuỗi.
      concatenates all the list’s items in a single string.
    • >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      40 mang lại các mục mới do áp dụng một cuộc gọi
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      41 có thể gọi cho từng mục trong danh sách cơ bản.
      yields new items that result from applying an
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      41 callable to each item in the underlying list.
    • >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      42 mang lại tất cả các mục trả về
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      43 khi gọi
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      44 trên chúng.
      yields all the items that return
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      43 when calling
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      44 on them.
    • >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      45 gọi
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      46 trên mỗi mục trong danh sách cơ bản để tạo ra một số tác dụng phụ.
      calls
      >>> from string_list import StringList
      
      >>> data = StringList([1, 2, 2, 4, 5])
      >>> data
      ['1', '2', '2', '4', '5']
      
      >>> data.append(6)
      >>> data
      ['1', '2', '2', '4', '5', '6']
      
      >>> data.insert(0, 0)
      >>> data
      ['0', '1', '2', '2', '4', '5', '6']
      
      >>> data.extend([7, 8, 9])
      >>> data
      ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
      
      >>> data[3] = 3
      >>> data
      ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
      
      46 on every item in the underlying list to generate some side effect.

    Ở đây, một lớp thực hiện tất cả các tính năng mới này bằng cách phân lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1:

    # custom_list.py
    
    class CustomList(list):
        def join(self, separator=" "):
            return separator.join(str(item) for item in self)
    
        def map(self, action):
            return type(self)(action(item) for item in self)
    
        def filter(self, predicate):
            return type(self)(item for item in self if predicate(item))
    
        def for_each(self, func):
            for item in self:
                func(item)
    

    Phương thức

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    39 trong
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    49 lấy ký tự phân cách làm đối số và sử dụng nó để kết hợp các mục trong đối tượng danh sách hiện tại, được biểu thị bằng
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9. Để làm điều này, bạn sử dụng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    51 với biểu thức trình tạo làm đối số. Biểu thức trình tạo này chuyển đổi mọi mục thành một đối tượng chuỗi bằng cách sử dụng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    52.

    Phương thức

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    53 trả về một đối tượng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    49. Để xây dựng đối tượng này, bạn sử dụng biểu thức trình tạo áp dụng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    41 cho mọi mục trong đối tượng hiện tại,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9. Lưu ý rằng hành động có thể là bất kỳ cuộc gọi nào có thể gọi một mục làm đối số và trả về một mục được chuyển đổi.

    Phương thức

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    57 cũng trả về một đối tượng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    49. Để xây dựng đối tượng này, bạn sử dụng biểu thức máy phát mang lại các mục mà
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    44 trả về
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    43. Trong trường hợp này,
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    44 phải là hàm có giá trị boolean trả về
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    43 hoặc
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    63 tùy thuộc vào các điều kiện nhất định được áp dụng cho mục đầu vào.

    Cuối cùng, phương thức

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    64 gọi
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    46 trên mỗi mục trong danh sách cơ bản. Cuộc gọi này không trả lời bất cứ điều gì ngoài việc kích hoạt một số tác dụng phụ, như bạn sẽ thấy bên dưới.

    Để sử dụng lớp này trong mã của bạn, bạn có thể làm một cái gì đó như sau:

    >>>

    >>> from custom_list import CustomList
    
    >>> words = CustomList(
    ...     [
    ...         "Hello,",
    ...         "Pythonista!",
    ...         "Welcome",
    ...         "to",
    ...         "Real",
    ...         "Python!"
    ...     ]
    ... )
    
    >>> words.join()
    'Hello, Pythonista! Welcome to Real Python!'
    
    >>> words.map(str.upper)
    ['HELLO,', 'PYTHONISTA!', 'WELCOME', 'TO', 'REAL', 'PYTHON!']
    
    >>> words.filter(lambda word: word.startswith("Py"))
    ['Pythonista!', 'Python!']
    
    >>> words.for_each(print)
    Hello,
    Pythonista!
    Welcome
    to
    Real
    Python!
    

    Trong các ví dụ này, lần đầu tiên bạn gọi

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    39 vào
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    67. Phương pháp này trả về một chuỗi duy nhất kết quả từ việc kết hợp tất cả các mục trong danh sách cơ bản.

    Cuộc gọi đến

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    53 trả về một đối tượng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    49 chứa các từ trên đường. Chuyển đổi này là kết quả của việc áp dụng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    70 cho tất cả các mục trong
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    67. Phương pháp này hoạt động khá giống với hàm
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    72 tích hợp. Sự khác biệt chính là thay vì trả lại danh sách, hàm
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    72 tích hợp trả về một trình lặp lại mang lại các mục chuyển đổi một cách uể oải.

    Phương thức

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    57 lấy hàm
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    75 làm đối số. Trong ví dụ, hàm
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    75 này sử dụng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    77 để chọn những từ bắt đầu bằng tiền tố
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    78. Lưu ý rằng phương pháp này hoạt động tương tự như hàm
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    79 tích hợp, trả về trình lặp thay vì danh sách.

    Cuối cùng, cuộc gọi đến

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    64 trên
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    67 in mỗi từ lên màn hình như là một tác dụng phụ của việc gọi
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    82 trên mỗi mục trong danh sách cơ bản. Lưu ý rằng hàm được chuyển đến
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    64 sẽ lấy một mục làm đối số, nhưng nó không nên trả lại bất kỳ giá trị hiệu quả nào.

    Bạn cũng có thể thực hiện

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    49 bằng cách kế thừa từ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 thay vì từ
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1. Trong trường hợp này, bạn không cần phải thay đổi triển khai nội bộ, chỉ là lớp cơ sở:

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    0

    Lưu ý rằng trong ví dụ này, bạn chỉ cần thay đổi lớp cha. Không cần phải sử dụng trực tiếp

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80. Tuy nhiên, bạn có thể sử dụng nó nếu bạn muốn. Ưu điểm là bạn sẽ cung cấp nhiều bối cảnh hơn cho các nhà phát triển khác đọc mã của bạn:

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    1

    Trong phiên bản mới này của

    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    88, thay đổi duy nhất là bạn đã thay thế
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    9 bằng
    >>> from string_list import StringList
    
    >>> data = StringList([1, 2, 2, 4, 5])
    >>> data
    ['1', '2', '2', '4', '5']
    
    >>> data.append(6)
    >>> data
    ['1', '2', '2', '4', '5', '6']
    
    >>> data.insert(0, 0)
    >>> data
    ['0', '1', '2', '2', '4', '5', '6']
    
    >>> data.extend([7, 8, 9])
    >>> data
    ['0', '1', '2', '2', '4', '5', '6', '7', '8', '9']
    
    >>> data[3] = 3
    >>> data
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    90 để làm rõ rằng bạn làm việc với lớp con
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25. Thay đổi này làm cho mã của bạn rõ ràng hơn.

    Xem xét hiệu suất: # string_list.py from collections import UserList class StringList(UserList): def __init__(self, iterable): super().__init__(str(item) for item in iterable) def __setitem__(self, index, item): self.data[index] = str(item) def insert(self, index, item): self.data.insert(index, str(item)) def append(self, item): self.data.append(str(item)) def extend(self, other): if isinstance(other, type(self)): self.data.extend(other) else: self.data.extend(str(item) for item in other) 1 so với # string_list.py class StringList(list): def __init__(self, iterable): super().__init__(str(item) for item in iterable) def __setitem__(self, index, item): super().__setitem__(index, str(item)) def insert(self, index, item): super().insert(index, str(item)) def append(self, item): super().append(str(item)) def extend(self, other): if isinstance(other, type(self)): super().extend(other) else: super().extend(str(item) for item in other) 25

    Cho đến thời điểm này, bạn đã học được cách tạo các lớp giống như danh sách của riêng mình bằng cách kế thừa từ

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 hoặc
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25. Bạn cũng biết rằng sự khác biệt có thể nhìn thấy duy nhất giữa hai lớp này là
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 phơi bày thuộc tính
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80, có thể tạo điều kiện thuận lợi cho quá trình mã hóa.

    Trong phần này, bạn sẽ xem xét một khía cạnh có thể quan trọng khi quyết định sử dụng

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 hay
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 để tạo các lớp giống như danh sách tùy chỉnh của bạn. Đó là hiệu suất!

    Để đánh giá nếu có sự khác biệt về hiệu suất giữa các lớp kế thừa từ

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 so với
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, bạn sẽ sử dụng lớp
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    58. Đi trước và tạo một tệp Python mới chứa mã sau:

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    2

    Hai lớp này hoạt động giống nhau. Tuy nhiên, họ khác nhau trong nội bộ.

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    03 kế thừa từ
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 và việc thực hiện nó dựa trên
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    81. Ngược lại,
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    06 kế thừa từ
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 và việc triển khai của nó dựa trên thuộc tính
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80 nội bộ.

    Để so sánh hiệu suất của hai lớp này, bạn nên bắt đầu bằng thời gian các hoạt động danh sách tiêu chuẩn, chẳng hạn như khởi tạo. Tuy nhiên, trong các ví dụ này, cả hai bộ khởi tạo đều tương đương, vì vậy chúng nên thực hiện giống nhau.

    Đo thời gian thực hiện của các chức năng mới cũng hữu ích. Ví dụ: bạn có thể kiểm tra thời gian thực hiện của

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    66. Đi trước và chạy mã sau:

    >>>

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    3

    Trong thử nghiệm hiệu suất này, bạn sử dụng mô -đun

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    10 cùng với hàm
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    11 để đo thời gian thực hiện của một đoạn mã. Mã đích bao gồm các cuộc gọi đến
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    66 trên các trường hợp
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    03 và
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    06 bằng một số dữ liệu mẫu.

    Sự khác biệt về hiệu suất giữa lớp dựa trên

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 và lớp dựa trên
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 hầu hết không tồn tại trong ví dụ này.

    Thông thường, khi bạn tạo một lớp giống như danh sách tùy chỉnh, bạn sẽ mong đợi các lớp con là

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 hoạt động tốt hơn so với các lớp con của
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25. Tại sao? Bởi vì
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 được viết bằng C và được tối ưu hóa cho hiệu suất, trong khi
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 là một lớp trình bao bọc được viết bằng Python Pure Python.

    Tuy nhiên, trong ví dụ trên, có vẻ như giả định này không hoàn toàn đúng. Vì lý do này, để quyết định siêu lớp nào là tốt nhất cho trường hợp sử dụng cụ thể của bạn, hãy đảm bảo chạy thử nghiệm hiệu suất.

    Hiệu suất sang một bên, kế thừa từ

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 được cho là cách tự nhiên trong Python, chủ yếu là vì
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 có sẵn trực tiếp cho các nhà phát triển Python như một lớp học tích hợp. Ngoài ra, hầu hết các nhà phát triển Python sẽ quen thuộc với các danh sách và các tính năng tiêu chuẩn của họ, điều này sẽ cho phép họ viết các lớp giống như danh sách nhanh hơn.

    Ngược lại, lớp

    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 sống trong mô -đun
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26, có nghĩa là bạn sẽ phải nhập nó nếu bạn muốn sử dụng nó trong mã của mình. Ngoài ra, không phải tất cả các nhà phát triển Python đều biết về sự tồn tại của
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25. Tuy nhiên,
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 vẫn có thể là một công cụ hữu ích vì sự tiện lợi của việc truy cập thuộc tính
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    80, có thể tạo điều kiện cho việc tạo các lớp giống như danh sách tùy chỉnh.

    Sự kết luận

    Bây giờ bạn đã học được cách tạo các lớp giống như danh sách tùy chỉnh với các hành vi đã sửa đổi và mới. Để làm điều này, bạn đã trực tiếp lớp học được phân lớp

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 tích hợp. Thay vào đó, bạn cũng đã kế thừa từ lớp
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25, có sẵn trong mô -đun
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26.custom list-like classes with modified and new behaviors. To do this, you’ve subclassed the built-in
    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 class directly. As an alternative, you’ve also inherited from the
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 class, which is available in the
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    26 module.

    Kế thừa từ

    # string_list.py
    
    from collections import UserList
    
    class StringList(UserList):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            self.data[index] = str(item)
    
        def insert(self, index, item):
            self.data.insert(index, str(item))
    
        def append(self, item):
            self.data.append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                self.data.extend(other)
            else:
                self.data.extend(str(item) for item in other)
    
    1 và phân lớp
    # string_list.py
    
    class StringList(list):
        def __init__(self, iterable):
            super().__init__(str(item) for item in iterable)
    
        def __setitem__(self, index, item):
            super().__setitem__(index, str(item))
    
        def insert(self, index, item):
            super().insert(index, str(item))
    
        def append(self, item):
            super().append(str(item))
    
        def extend(self, other):
            if isinstance(other, type(self)):
                super().extend(other)
            else:
                super().extend(str(item) for item in other)
    
    25 đều là những chiến lược phù hợp để tiếp cận vấn đề tạo ra các lớp giống như danh sách của riêng bạn trong Python.

    Trong hướng dẫn này, bạn đã học được cách:

    • Tạo các lớp giống như danh sách bằng cách kế thừa từ lớp
      # string_list.py
      
      from collections import UserList
      
      class StringList(UserList):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              self.data[index] = str(item)
      
          def insert(self, index, item):
              self.data.insert(index, str(item))
      
          def append(self, item):
              self.data.append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  self.data.extend(other)
              else:
                  self.data.extend(str(item) for item in other)
      
      1 tích hợpbuilt-in
      # string_list.py
      
      from collections import UserList
      
      class StringList(UserList):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              self.data[index] = str(item)
      
          def insert(self, index, item):
              self.data.insert(index, str(item))
      
          def append(self, item):
              self.data.append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  self.data.extend(other)
              else:
                  self.data.extend(str(item) for item in other)
      
      1 class
    • Xây dựng các lớp giống như danh sách bằng cách phân lớp
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      25 từ mô-đun
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      26
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      25
      from the
      # string_list.py
      
      class StringList(list):
          def __init__(self, iterable):
              super().__init__(str(item) for item in iterable)
      
          def __setitem__(self, index, item):
              super().__setitem__(index, str(item))
      
          def insert(self, index, item):
              super().insert(index, str(item))
      
          def append(self, item):
              super().append(str(item))
      
          def extend(self, other):
              if isinstance(other, type(self)):
                  super().extend(other)
              else:
                  super().extend(str(item) for item in other)
      
      26 module

    Bây giờ, bạn đã chuẩn bị tốt hơn để tạo danh sách tùy chỉnh của riêng mình, cho phép bạn tận dụng toàn bộ sức mạnh của loại dữ liệu hữu ích và phổ biến này trong Python.

    Loại lớp nào là một danh sách trong Python?

    Danh sách là một trong 4 loại dữ liệu tích hợp trong Python được sử dụng để lưu trữ các bộ sưu tập dữ liệu, 3 loại khác là tuple, set và từ điển, tất cả đều có phẩm chất và cách sử dụng khác nhau.one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

    Là liệt kê một lớp hoặc đối tượng trong Python?

    Chúng ta có thể tạo danh sách đối tượng trong Python bằng cách nối thêm các phiên bản lớp vào danh sách.Bằng cách này, mọi chỉ mục trong danh sách đều có thể trỏ đến các thuộc tính và phương thức của lớp và có thể truy cập chúng.Nếu bạn quan sát nó một cách chặt chẽ, một danh sách các đối tượng hoạt động giống như một mảng các cấu trúc trong C.. By this, every index in the list can point to instance attributes and methods of the class and can access them. If you observe it closely, a list of objects behaves like an array of structures in C.

    Loại danh sách nào là một danh sách Python?

    Danh sách Python là gì?Một danh sách là một container Python được đặt hàng và có thể thay đổi, là một trong những cấu trúc dữ liệu phổ biến nhất trong Python.Để tạo một danh sách, các phần tử được đặt bên trong dấu ngoặc vuông ([]), được phân tách bằng dấu phẩy.Như được hiển thị ở trên, danh sách có thể chứa các yếu tố của các loại khác nhau cũng như các yếu tố trùng lặp.an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets ([]), separated by commas. As shown above, lists can contain elements of different types as well as duplicated elements.

    Danh sách là một đối tượng trong Python?

    Danh sách cũng là đối tượng.Một phương pháp quan trọng cho danh sách là phụ lục (mục).. An important method for lists is append(item).