beginner question: extending python types

Terry Reedy tjreedy at udel.edu
Thu May 16 09:30:51 EDT 2002


> > > * Uwe Mayer
> > > |>I found Python lacking the type of a "set". i.e. an unordered
list.

> > Jeremy Yallop wrote:
> > > Use a dictionary, with the same value (1) for every key.

> Bob Horvath <usenet at horvath.com> writes:
> > Is there any significance to the value 1?

"Michael Hudson" <mwh at python.net> wrote in message
> In this context, no.  It might be a tad confusing to have something
> that was false, so None, 0 and () might be bad choices.  But I don't
> think there's a good reason.

For choosing between 0 and 1, confusion is the only reason to choose
1.  However, in the current C implementation, there are reasons to
reject the others.  'Small' ints are preallocated and handled
specially (more efficiently) by the compiler.  A set with 100000
members would have 100000 pointers to the same preallocated object.
The same would be true for None, but 'None' would be looked up in both
module and builtin dicts 100000 times.  '{}' or '()' would generate
100000 empty and useless objects.

If/when None is made a keyword, it *might* become the more efficient
choice since it would then be special-cased even earlier than 0/1.

Terry J. Reedy








More information about the Python-list mailing list