indexed property? Can it be done?

Charles Hixson charleshixsn at earthlink.net
Tue May 8 00:07:27 EDT 2012


On 05/07/2012 08:33 PM, Chris Rebert wrote:
> On Mon, May 7, 2012 at 8:15 PM, 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]
>>      
> Why are you using a single opaque list instead of separate, meaningful
> attributes for each datum?
>
> Cheers,
> Chris
>
>    
Because that's the most reasonable implementation.  The various list 
items would not be the same from instance to instance.  I could pull the 
top-level list items off as separate variables, but this would leave the 
problem exactly where it is, and ordinary properties allow me to address 
the entries like id, and dlu without problem.  But the list variables 
are a separate problem, and not so readily soluble.

FWIW, if I must I can operate with only a list, and define functions to 
do the access and manipulation.  I'd rather not, which is why I'm 
exploring whether an indexed property is feasible.

Note that I *could* return, e.g., the entire keys list, but I'd rather 
use a bit more data protection than that.  Which is what an indexed 
setter property would allow.  And node.key[3] is as fully informative a 
name as I can construct for that entry.  But the number of items in the 
keys list varies from node to node, so defining a key3 property is not 
reasonable.  (Besides, that's so ugly that I'd rather use a function, 
even though indexing in Python really should be done with brackets 
rather than parens.)

-- 
Charles Hixson




More information about the Python-list mailing list