|
From: Nicola L. <ni...@te...> - 2005-11-25 21:35:12
|
[Sorry for the lateness, it's been a busy week, and it's not over yet.] > What about this suggestion. It sounds a little esoteric to me - but if > you think it's worth the effort I'll do it. I am not sure I understand it (did not yet read the whole comp.lang.python thread). Can you give a few usage examples? > We could implement the ``keys`` and ``values`` methods as custom objects > with all the sequence methods. (With error checking so you can't mutate > the keys so that they don't reflect the internal contents of the odict). Do you mean that the ``keys`` and ``values`` methods *return* custom objects? > We would also implement ``__getitem__``, ``__setitem__``, and > ``__call__`` such that you can directly index (slice, delete, etc) > members as if it was a list. Do you mean as these? k4 = d1.keys()[4] d1.values()[4] = v4 > You can also call it as if it was a method > - including passing it a sequence to replace the current keys with. > > We would presumably have to implement ``__setattr__`` on the main > OrderedDict so that : > > d1 = OrderedDict(sequence_of_tuples) > d1.keys = a_list > > is the same as : > > d1 = OrderedDict(sequence_of_tuples) > d1.keys(a_list) If the two forms are equivalent, I'd say just do the first... > What should ``d1.keys(a_list)`` return ? (The same as d1.keys() I guess > - except with the new key set). ...so that we dodge this question. :-) > We (I) would still implement slicing, insert, reverse, and sort for the > OrderedDict class itself. What is there to sort? Isn't it already sorted? :-) > Fredrik Lundh pointed out the performance impact of adding to many > layers above the C implementation. I have no idea how relevant that is > to the suggestion I've just made. :-) (How many method calls would > indexing or assigning to the ``keys`` object actually result in 'under > the hood' ?) I'm not sure I grasp the last sentence. Performance is good, and I generally like *some* optimization, but in this context convenience and versatility, without excess, do override performance concerns. -- Nicola Larosa - ni...@te... She was up the learning curve like a mountain goat. -- WasterDave on Slashdot, October 2005 |