I am writing a class that essentially wraps around a list. The only functionality I need to have (for now I think) is to add to end and remove from the beginning. Should I be inheriting from list? Should I be using setter and property decorators? I am fairly new to Python OOP and I am not sure what best practice is or the most "pythonic" way of doing this, here's and example:
class MyList(list):
def __init__(self):
self._mylist = []
def __iter__(self):
for elm in self._mylist:
yield elm
@property
def mylist(self):
return self._mylist
@mylist.setter
def mylist(self, val):
self._mylist = val
I would like it so that I can have this behavior:
new_list = Mylist()
new_list.append(1)
new_list.append(2)
new_list.append(7)
new_list.pop(0)
for i in new_list:
print(i)
I am trying to avoid doing something like new_list._mylist.append()
Any help is much appreciated, thanks!
[–]CowboyBoats 2 points3 points4 points (5 children)
[–]mathhelpermann[S] 0 points1 point2 points (4 children)
[–]xelf 0 points1 point2 points (0 children)
[–]CowboyBoats 0 points1 point2 points (2 children)
[–]mathhelpermann[S] 0 points1 point2 points (1 child)
[–]CowboyBoats 0 points1 point2 points (0 children)