:
class CustomList(list):
def __repr__(self):
if len(self) == 0:
return '{}'
elif len(self) == 1:
return '{%s}' % self[0]
elif len(self) == 2:
return '{%s and %s}' % (self[0], self[1])
else:
return '{' + ', '.join(str(x) for x in self[:-1]) + ' and %s}' % self[-1]
>>> my_list = CustomList()
>>> my_list
{}
>>> my_list.append(1)
>>> print(my_list)
{1}
>>> my_list.append('spam')
>>> print(my_list)
{1 and spam}
>>> my_list.append('eggs')
>>> my_list.append('ham')
>>> print(my_list)
{1, spam, eggs and ham}
>>> my_list
{1, spam, eggs and ham}
, list
, .