indexed property? Can it be done?
Ian Kelly
ian.g.kelly at gmail.com
Tue May 8 12:49:33 EDT 2012
On Tue, May 8, 2012 at 10:30 AM, Charles Hixson
<charleshixsn at earthlink.net> wrote:
> That depends on what you're doing. For many, perhaps most, purposes I would
> agree. Not for this one. And I couldn't use an internal dict, as the order
> in which the items of the sub-lists occur is significant. The sub-lists
> need to be lists, though they could be separated out as named variables
> (which would be lists).
It sounds like a collections.OrderedDict should do what you need. In
the cases where you really need to have a list of keys or values, you
can just use the .keys() and .values() methods.
> The ActiveState recipe *might* do what I want. I honestly can't tell after
> a brief study. But it's much too complex to be a worthwhile choice. I was
> hoping that I'd just overlooked one of the standard features of Python.
What do you find complex about it? The two class definitions are
something you would put in a library module to be imported. The rest
is just example / test code. The actual usage is no more complex than
a regular property:
class Node(object):
def __init__(self, nodeId, key, value, downRight, downLeft, parent):
dirty = True
dlu = utcnow()
self.node = [nodeId, downLeft, [key], [value],
[downRight], parent, dirty, dlu]
@itemproperty
def key(self, index):
return self.node[2][index]
@itemproperty
def value(self, index):
return self.node[3][index]
And of course you could also add setters and/or deleters if desired.
More information about the Python-list
mailing list