Listing 10 - Page 0: No Title

##############################################################################
# Python From Scratch
# Autor: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2024
# Site: https://pythonfromscratch.com
#
# File: listing\chapter 10\10.1446 - No Title.py
# Description: No Title
##############################################################################

from collections import UserList
class UniqueList(UserList):
    def __init__(self, elem_class, enumerable=None):
        super().__init__(enumerable)
        self.elem_class = elem_class
    def append(self, elem):
        self.verify_type(elem)
        if elem not in self.data:
            super().append(elem)
    def __setitem__(self, position, elem):
        self.verify_type(elem)
        if elem not in self.data:
            super().__setitem__(position, elem)

    def verify_type(self, elem):
        if not isinstance(elem, self.elem_class):
            raise TypeError("Invalid type")
Click here to download the file