I need a custom __reverse__
function for my class that I am deploying on App Engine, so it needs to work with Python 2.5
. Is there a __future__
import or a workaround I could use?
Subclassing list
won't work, as I need my class to be a subclass of dict
.
EDIT:
Using OrderedDict
will not solve the problems, because the dict
keys are not the same the same as the list
items.
This is the object I'm trying to create:
My object needs to provide the same attributes as a
list
, i.e. supportiter(obj)
andreverse(obj)
.The elements must be instances of a special third party class.
Each elements is associated with a key.
Internally, need to access these objects using their keys. That's why I'd put them in a mapping.
I've revised my implementation to be a list
subclass instead of a dict
subclass, so here's what I have now:
class Foo(list):
pat = {}
def __init__(self):
for app in APPS: # these are strings
obj = SpecialClass(app)
self.append(obj)
self.pat[app] = obj
def __getitem__(self, item):
# Use object as a list
if isinstance(item, int):
return super(Foo, self).__getitem__(item)
# Use object as a dict
if item not in self.pat:
# Never raise a KeyError
self.pat[item] = SpecialClass(None)
return self.pat[item]
def __setitem__(self, item, value):
if isinstance(item, int):
return self.pat.__setitem__(item, value)
return super(Foo).__setitem__(item, value)
EDIT 2:
Now that my class is a subclass of list
, my problem is resolved.