4

I would like to replace lists in a program with another class in order to obtain more information on the underlying data. It's easy enough to do the following replacement:

list = ObservableList  # new class, compatible with list

so that list([1, 2, 3]) returns an ObservableList, but this does not affect expressions like [1, 2, 3] or list comprehensions, which will still return a Python list. Is there any way to override these constructions so that they return an ObservableList as well?

Anthony Labarre
  • 2,745
  • 1
  • 28
  • 39
  • 2
    Ummm.. you can use some lib to handle stuff at byte-code level and modify [`BUILD_LIST`](https://docs.python.org/2/library/dis.html#opcode-BUILD_LIST) step somehow. But won't be worth it. It's better to be more explicit and check the type and change it to `ObservableList` wherever required. – Ashwini Chaudhary Jun 19 '18 at 11:33

0 Answers0