Re: empty object from C

Discussion in 'Python' started by Stefan Behnel, Dec 7, 2012.

  1. Eric Frederich, 07.12.2012 16:42:
    > From C, I'd like to call a Python function that takes an object and sets
    > some attributes on it.
    > Lets say this is the function...
    >
    > def foo(msg):
    > msg.bar = 123
    > msg.spam = 'eggs'
    >
    > How do I create an empty object in C?
    > In Python I would do something like this...
    >
    > class Msg(object):
    > pass
    >
    > ... and then instantiate an instance, and call the function.
    >
    > msg = Msg()
    > foo(msg)
    >
    > I know how to create an empty dictionary and I get get by with that, but
    > I'd like to create an object.


    Here's some Cython code that you can call from C (after linking it in and
    creating a Python interpreter at runtime):

    cdef public set_attributes(msg, int bar, char* spam):
    msg.bar = bar
    msg.spam = spam.decode('utf8') # or whatever you use

    However, how you'd use it depends on your actual C code. Personally, I'd
    drop C and write all Python interaction in Cython. Makes these things way
    easier.

    Here are some longer examples:

    https://github.com/cython/cython/tree/master/Demos/embed

    https://github.com/cython/cython/tree/master/Demos/callback

    Stefan
     
    Stefan Behnel, Dec 7, 2012
    #1
    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. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,039
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    820
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    583
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    665
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    569
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page