indexed property? Can it be done?
Dan Sommers
dan at tombstonezero.net
Mon May 7 23:44:05 EDT 2012
On Mon, 07 May 2012 20:15:36 -0700
Charles Hixson <charleshixsn at earthlink.net> wrote:
> class Node:
>
> def __init__(self, nodeId, key, value, downRight, downLeft,
> parent): dirty = True
> dlu = utcnow()
> self.node = [nodeId, downLeft, [key], [value],
> [downRight], parent, dirty, dlu]
>
> Note that node[3] is a list of keys (initially 1) and node[3] is a
> list of values, etc.
>
> What I'd like to do is to be able to address them thusly:
> k = node.key[2]
> v = node.value[2]
> but if there's a way to do this, I haven't been able to figure it
> out. Any suggestions?
Untested:
def __init__(self, nodeId, key, value, downRight, downLeft, parent):
dirty = True
dlu = utcnow()
self.node = [nodeId, downLeft, dict(key=value),
[downRight], parent, dirty, dlu]
Now you can use self.node[2][key] to get/set value.
But why not make the elements of node their own attributes?
Untested:
def __init__(self, nodeId, key, value, downRight, downLeft, parent):
self.dirty = True
self.dlu = utcnow()
self.nodeId = nodeId
self.downLeft = downLeft
self.downRight = downRight
self.values = dict(key=value)
self.parent = parent
And then you don't have to remember that node[2] is the key/value pairs
(note the typo (the two "3"s) in your original post). With each
attribute in its own, well, attribute, you can always use
node.values[key] to access the value associated with a particular key.
HTH,
Dan
More information about the Python-list
mailing list