Manipulating sets from the 2.4 C API?

Discussion in 'Python' started by Dave Opstad, Apr 11, 2006.

  1. Dave Opstad

    Dave Opstad Guest

    I just looked through the Python/C API reference for 2.4.3 and didn't
    see anything about using sets. I'd been expecting things like PySet_New,
    PySet_Check etc.

    If I want to handle sets should I just use a dictionary's keys and
    ignore the values, or is there some more explicit set support somewhere
    I'm not seeing?

    Thanks,
    Dave Opstad
     
    Dave Opstad, Apr 11, 2006
    #1
    1. Advertising

  2. On Tue, Apr 11, 2006 at 09:29:10AM -0700, Dave Opstad wrote:
    > I just looked through the Python/C API reference for 2.4.3 and didn't
    > see anything about using sets. I'd been expecting things like PySet_New,
    > PySet_Check etc.


    There is a public C API starting in 2.5, the progression for sets was
    pure-python in 2.3, C in 2.4, and polished in 2.5 after it was better
    understood how people use them. If Hettinger is around he'll correct me
    if that explanation is off.

    > If I want to handle sets should I just use a dictionary's keys and
    > ignore the values, or is there some more explicit set support somewhere
    > I'm not seeing?


    There are people that use sets from C-code but I don't know if they use
    the slotted methods, the 2.5 version, or a special build.

    -Jack
     
    Jack Diederich, Apr 11, 2006
    #2
    1. Advertising

  3. Dave Opstad wrote:
    > If I want to handle sets should I just use a dictionary's keys and
    > ignore the values, or is there some more explicit set support somewhere
    > I'm not seeing?


    Indeed, there is. To create a new set, do

    PyObject_Call(PySet_Type, "");

    To, say, invoke the add method, do

    PyObject_CallMethod(s, "add", "O", o);

    HTH,
    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Apr 11, 2006
    #3
  4. Em Ter, 2006-04-11 às 18:55 +0200, "Martin v. Löwis" escreveu:
    > Dave Opstad wrote:
    > > If I want to handle sets should I just use a dictionary's keys and
    > > ignore the values, or is there some more explicit set support somewhere
    > > I'm not seeing?

    >
    > Indeed, there is. To create a new set, do
    >
    > PyObject_Call(PySet_Type, "");
    >
    > To, say, invoke the add method, do
    >
    > PyObject_CallMethod(s, "add", "O", o);


    I don't know much about the C API, but I'll ask anyway: the methods,
    say, PySet, would be included for clarity/brevity or for performance
    reasons?

    --
    Felipe.
     
    Felipe Almeida Lessa, Apr 11, 2006
    #4
  5. Dave Opstad wrote:
    > I just looked through the Python/C API reference for 2.4.3 and didn't
    > see anything about using sets. I'd been expecting things like PySet_New,
    > PySet_Check etc.


    In Py2.4, there was not a custom set C API because the module was still
    ungoing significant development. For 2.4, the best way to access sets
    from C is to use the abstract API:

    s=PyObject_CallObject(PySet_Type, NULL) // s=set()
    PyObject_TypeCheck(o, &PySet_Type) // isinstance(o, set)
    PyObject_GetIter(s) // iter(s)
    PyObject_Hash(f) // hash of frozenset
    PyObject_Length(s) // len(s)
    PyNumber_Subtract(s,t) // s - t
    PyObject_CallMethod(s, "pop", NULL) // s.pop()


    > If I want to handle sets should I just use a dictionary's keys and
    > ignore the values, or is there some more explicit set support somewhere
    > I'm not seeing?


    In Py2.4, set objects are based on dictionaries so the performance is
    about the same, so the only advantage of using set objects instead of
    dictionaries is that they provide methods like union, intersection, etc
    which are not defined for dictionaries.

    In Py2.5, set objects are no longer based on dictionaries and tend to
    have better speed/space performance than equivalent dictionary code.
    There is also a more full-blown C API including PySet_New(),
    PySet_Size(), PySet_Add(), PySet_Contains(), ...
     
    Raymond Hettinger, Apr 11, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray
    Replies:
    2
    Views:
    414
  2. Shlomo Anglister
    Replies:
    1
    Views:
    433
    Default User
    Aug 2, 2004
  3. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    396
    Praveen, Tayal (IE10)
    Mar 17, 2005
  4. John123

    Profiling API or Membership API

    John123, Oct 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    399
    John123
    Oct 20, 2006
  5. George2

    platform specific API or C standard API

    George2, Nov 12, 2007, in forum: C Programming
    Replies:
    13
    Views:
    774
    Tor Rustad
    Nov 13, 2007
Loading...

Share This Page