Re: [Python-Dev] LinkedHashSet/LinkedHashMap equivalents
Steven Bethard wrote:
Thomas Heller
wrote: [About an ordered dictionary]
Well, that was basically the question I posed. So far I've seen only one use for it, and that one is better served by adding a function to itertools. What use do you have for it other than filtering duplicates from a list while retaining order?
Steve
Using a LinkedHashMap generally cuts down in the amount of apparent randomness in a program. This is especially helpful when it comes time to debug a really complicated program by diffing log files, since it prevents slightly different maps from having wildly different iteration orders. Often using a plain HashMap can introduce enough randomness to make two otherwise similar log files nearly impossible to compare. The other use case I have is for dealing with data where the iteration order doesn't matter to the program but it does matter to users. For instance, consider the ConfigParser's write method. Any ordering of values in the output is functionally equivalent, but the original data is likely to have come from a file that was arranged in some meaningful order, and it would be nice to preserve that order, especially if it can be done with no extra effort. --jw
John Williams wrote:
The other use case I have is for dealing with data where the iteration order doesn't matter to the program but it does matter to users. For instance, consider the ConfigParser's write method. Any ordering of values in the output is functionally equivalent, but the original data is likely to have come from a file that was arranged in some meaningful order, and it would be nice to preserve that order, especially if it can be done with no extra effort.
That is a good example, IMO. But then, in the specific case, the dictionary for each section is created deeply inside ConfigParser, with the lines cursect = {'__name__': sectname} self._sections[sectname] = cursect So this uses a builtin dict, and there is no easy way to override it for the application. Of course, given your reasoning, ConfigParser *should* use an OrderedDictionary (probably both for cursect and for self._sections), in which case this would all be transparent to the application. Regards, Martin
participants (2)
-
"Martin v. Löwis"
-
John Williams