Details about pythons set implementation

Hrvoje Niksic hniksic at xemacs.org
Fri Jan 4 10:05:58 EST 2008


Achim Domma <domma at procoders.net> writes:

> I'm interested in details about how sets are implemented in python.
> They seem to be quite fast and I found some remarks who state, that
> the implementation is highly optimized. I need to implemented sets
> in C/C++ and need a starting point on how to do it right. Could
> somebody give me a starting point?

You can simply look at the implementation, Objects/setobject.c in the
Python source code.  Most that it's mostly copy-paste from the dict
implementation (dictobject.c) and that both are quite involved and
optimized for the use by Python.  They're not general implementation
of sets from use in C.

The "highly optimized" remarks should be understood in the context of
Python, not in the context of other C and C++ set libraries.  I don't
know how well Python sets compare to other set libraries, but I doubt
that it's much faster than the median (which "highly optimized" could
be understood to imply).

BTW if you're using C++, why not simply use std::set?  If you need it
called from C, you can wrap the needed methods in a C-accessible API.



More information about the Python-list mailing list