1

I wanted to try and replace python's mutable datastructures with immutable ones (similar to clojure).

Subclassing the builtin list class works and you can override __builtin__.list but I haven't found any way of changing what builtin syntax refers to

__builtin__.list = ImmutableListClass

my_list = [1, 2, 3]
print type(my_list)

>>> <type 'list'>

Is there any way to change this behavior or is it hardcoded in the interpreter?

Disclaimer: I'm aware this should never be done in a serious project and it's purely for my own entertainment.

felixbr
  • 843
  • 1
  • 7
  • 18

2 Answers2

0

You cant since these module are written in C, and hence have .so extensions. You can however try using forbidden fruit:

http://clarete.li/forbiddenfruit/

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • I accept this answer, because it seems that the only possibility is to use forbidden fruit to modify (instead of replace) the existing `list` type which is used by the `[1, 2, 3]` syntax. – felixbr Jan 30 '15 at 13:23
-2

Yes, it is possible. This answer does something similar, albeit with dicts, so I imagine that it could be adapted. However, I'm far too frightened to try.

Community
  • 1
  • 1
Tom
  • 522
  • 3
  • 13