[Python-Dev] frozenset C API? (original) (raw)

Bill Janssen janssen at parc.com
Tue Sep 4 21:31:09 CEST 2007


Raymond, thanks for the note.

You can create a frozenset from any iterable using PyFrozenSetNew().

If you don't have an iterable and want to build-up the frozenset one element at a time, the approach is to create a regular set (or some other mutable container), add to it, then convert it to a frozenset when you're done: s = PySetNew(NULL); PySetAdd(s, obj1); PySetAdd(s, obj2); PySetAdd(s, obj3); f = PyFrozenSetNew(s); PyDECREF(s);

This is essentially the same thing I mentioned, except using a set instead of a list as the iterable.

I'm just a tad annoyed at the fact that I know at set creation time exactly how many elements it's going to have, and this procedure strikes me as a somewhat inefficient way to create that set. Just tickles my "C inefficiency" funnybone a bit :-).

The API you propose doesn't work because sets and frozensets are not indexed like tuples and lists. Accordingly, sets and frozensets have a C API that is more like dictionaries. Since dictionaries are not indexable, they also cannot have an API like the one you propose:

PyDictNEW(int) => PySetObject * PyDictSETITEM(s, index, key, value)

Didn't really mean to propose "PyDict_SET_ITEM(s, index, key, value)", should have been

   PyDict_SET_ITEM(s, index, value)

But your point is still well taken. How about this one, though:

 PyDict_NEW(int) => PySetObject *
 PyDict_ADD(s, value)

ADD would just stick value in the next empty slot (and steal its reference).

Bill



More information about the Python-Dev mailing list