-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunnylist.py
More file actions
32 lines (27 loc) · 1.04 KB
/
funnylist.py
File metadata and controls
32 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class FunnyList(list):
def __init__(self, item):
"""Allows us to create a FunnyList not only from a list,
but ALSO from a single element
"""
if type(item) == list:
return super().__init__(item)
else:
return super().__init__([item])
def __eq__(self, other):
"""Check for equality without concern for order.
If the sorted versions of two FunnyLists are the
same, then we deem the FunnyLists to be the same.
"""
return sorted(self) == sorted(other)
def __ne__(self, other):
return sorted(self) != sorted(other)
def __add__(self, thing):
"""Add to a FunnyList. Distinguish between adding a
list/FunnyList, and something else.
"""
if not isinstance(thing, list):
return FunnyList(super().__add__([thing]))
return FunnyList(super().__add__(thing))
def __iadd__(self, thing):
"""Same as above except this is += instead of +."""
return self + thing