[Python-Dev] Toowtdi: Datatype conversions

Martin v. Loewis martin at v.loewis.de
Sat Jan 3 21:21:26 EST 2004

Raymond Hettinger wrote:

> Put another way, which is preferable:
>    list(d.iterkeys())    vs.   d.keys()
>    list(d.itervalues())  vs.   d.values()
>    list(d.iteritems())   vs.   d.items()

Clearly, the direct methods. Why do you ask?

> The alternative API is:
> bag.asList()
> bag.asDict()
> bag.asSet()
> bag.unique()

For bags, I would say that this API is most appropriate, from
a pure Python point of view. Of course, as you are modelling
Smalltalk, you should let yourself guide by the methods that
Smalltalk provides (which is just asSet, asArray, and sortedByCount,

It appears that Smalltalk has no way of converting the Bag to
a Dictionary. I may be missing something here, since that
is an obvious conversion - but then, also an unnecessary one.

> The former has two fewer methods.  The latter has much better
> performance but won't support casts to subclasses of list/set/dict.

Users in need of such conversions could always convert the result
of some .as method.

>>   list(a_set)
>>is the most obvious way, and it should work fastest.
> Right!  Unfortunately, it can never be as fast as a set.elements() 
> method -- the underlying d.keys() method has too many advantages 

Well, list construction could special-case sets.

> That leaves a question as to how to best empower the dictionary
> constructor.

Why is that an interesting question, again (for the set case)?


More information about the Python-Dev mailing list