I'd like to change the behavior of Python's list displays so that instead of producing a list
, they produce a subclass of list
that I've written. (Note: I don't think this is a good idea; I'm doing it for fun, not actual use.)
Here's what I've done:
old_list = list
class CallableList(old_list):
def __init__(self, *args):
old_list.__init__(self)
for arg in args:
self.append(arg)
def __call__(self, start, end=None):
if end:
return self[start:end]
return self[start]
list = CallableList
Once that's done, this returns the third element of the list:
x = list(1, 2, 3)
print x(2)
but this still gives an error:
x = [1, 2, 3]
print x(2)
The error is pretty straightforward:
Traceback (most recent call last):
File "list_test.py", line 23, in <module>
print x(2)
TypeError: 'list' object is not callable
I think there's probably no way of doing this, but I can't find anything that says so definitively. Any ideas?