Subclass Python list to Validate New Items

后端 未结 6 1456
广开言路
广开言路 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:30

    The general approach would be to create your own class inheriting vom list and overwriting the specific methods like append, extend etc. This will probably also include magic methods of the Python list (see this article for details: http://www.rafekettler.com/magicmethods.html#sequence).

    For validation, you will need to overwrite __setitem__(self, key, value)

提交回复
热议问题