View Single Post
Old Jun 3rd, 2006, 6:06 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
This seems an odd request - would it not be more readable to refer to the index by obj.index instead of obj()?

Regardless, if I'm understanding you right, you can get the functionality you want by subclassing the int type:
class keep_index(int):
    def __new__(cls, value, index):
        obj = int.__new__(cls, value)
        obj.index = index
        return obj
    def __call__(self):
        return self.index
The int type appears to use __new__ for setup, rather than __init__, which is why I had to override __new__ in order to get it to accept two parameters.
Arevos is offline   Reply With Quote