Subclass Python list to Validate New Items

后端 未结 6 1457
广开言路
广开言路 2021-01-13 14:46

I want a python list which represents itself externally as an average of its internal list items, but otherwise behaves as a list. It should raise a TypeError i

6条回答
  •  我在风中等你
    2021-01-13 15:36

    Actually the best answer may be: don't.

    Checking all objects as they get added to the list will be computationally expensive. What do you gain by doing those checks? It seems to me that you gain very little, and I'd recommend against implementing it.

    Python doesn't check types, and so trying to have a little bit of type checking for one object really doesn't make a lot of sense.

提交回复
热议问题