如何访问实例变量items以打印出每个类的唯一项目列表?

2024-04-19 10:50:34 发布

您现在位置:Python中文网/ 问答频道 /正文

class Trip(object):
    'a class that abstracts a trip to a destination'

    def __init__(self, destination='Nowhere', items=[] ):
        'Initialize the Trip class'
        self.destination = destination
        self.items = items

class DayTrip(Trip):
    'a class that abstracts a trip to a destination on a specific day'

    def __init__(self, destination='Nowhere', items=[] , day='1/1/2019' ):
        'Initialize the Trip class'
        self.destination = destination
        self.items = items
        self.day = day

class ExtendedTrip(Trip):
    'a class that abstracts a trip to a destination on between two dates'

    def __init__(self, destination='Nowhere', items=[] , day='1/1/2019' , 
endDay = '12/31/2019' ):
        'Initialize the ExtendedTrip class'
        self.destination = destination
        self.items = items
        self.day = day
        self.endDay = endDay

def luggage(lst):

我想让行李从每堂课上取下所有物品,并打印出一张打印出所有独特物品的清单

以下是一些示例输入:

trip1 = Trip('Basement', ['coat','pants','teddy bear'])

trip2 = DayTrip('Springfield',['floss', 'coat','belt'], '2/1/2018')

trip3 = ExtendedTrip('Mars',['shampoo', 'coat','belt'], '4/1/2058')

trip4 = ExtendedTrip()

每次行程都会附加到一个列表中,然后它将获取一组项目列表,这些项目列表将打印以下内容:

{'pants', 'belt', 'toothbrush', 'floss', 'shampoo', 'teddy bear', 'coat'}

Tags: toselfthatinitdefitemsdestinationclass