What to consider before subclassing list?

后端 未结 4 1233
闹比i
闹比i 2020-12-03 17:04

I was recently going over a coding problem I was having and someone looking at the code said that subclassing list was bad (my problem was unrelated to that class). He said

4条回答
  •  一向
    一向 (楼主)
    2020-12-03 17:52

    There are no benefits to subclassing list. None of the methods will use any methods you override, so you can have unexpected bugs. Further, it's very often confusing doing things like self.append instead of self.foos.append or especially self[4] rather than self.foos[4] to access your data. You can make something that works exactly like a list or (better) howevermuch like a list you really want while just subclassing object.

提交回复
热议问题