Ordering python sets

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Oct 25 05:21:05 EDT 2008


On Sat, 25 Oct 2008 08:58:18 +0000, Lie Ryan wrote:

> <anotherrandommusing>
> Since python is dynamic language, I think it should be possible to do
> something like this:
> 
> a = list([1, 2, 3, 4, 5], implementation = 'linkedlist')
> b = dict({'a': 'A'}, implementation = 'binarytree') 
> c = dict({'a': 'A'}, implementation = 'binarytree')

Oh I hope not. I think you have mistaken "dynamic" for "chaotic".

When I see a dict, I want to know that any two dicts work the same way. I 
don't want to have to search the entire project's source code to find out 
if it is a dict implemented as a hash table with O(1) lookups, or a dict 
implemented as a binary tree with O(log N) lookups, or a dict implemented 
as a linear array with O(N) lookups.

If I wanted that sort of nightmare, I can already do it by shadowing the 
builtin:

dict = binarytree
D = dict({'a': 'A'})  # make a binary tree

There is no possible good that come from this suggestion. The beauty of 
Python is that the built-in data structures (list, dict, set) are 
powerful enough for 99% of uses[1], and for the other 1%, you can easily 
and explicitly use something else.

But *explicitly* is the point. There's never any time where you can do 
this:

type(mydict) is dict

and not know exactly what performance characteristics mydict will have. 
(Unless you shadow dict or type, or otherwise do something that breaks 
the rules.) You never need to ask, "Okay, it's a dict. What sort of dict?"

If you want a binary tree, ask for a binary tree.






[1] Your mileage may vary.


-- 
Steven



More information about the Python-list mailing list