Python C api: create a new object class

lallous lallous at lgwm.org
Tue Nov 10 14:09:54 EST 2009


Hello

I have 3 questions, hope someone can help:

1)
How can I create an instance class in Python, currently I do:

class empty:
  pass

Then anytime I want that class (which I treat like a dictionary):

o = empty()
o.myattr = 1
etc....

Is there is a one line syntax to instantiate an instance?

Any other ways than this:
o = new.classobj('object', (), {})

2)

How can I, similarly, create an object "o" in C api:

PyObject *o = what_to_call(....)

....
PyObject_SetAttrString(o, "attrname", py_val)

...

One way I found was first calling PyClass_New() (to create an empty class) 
and then passing the return value to PyInstance_NewRaw to get a new instance 
of that empty class.

Can I achieve the same otherwise?

3)

Given a PyObject* is there is a way to tell if one can call 
PyObject_SetAttrString() on that object w/o getting an error?

For example, before calling a PyObject* one can see if it is callable, but 
can I test if an object supports setattr?

(from C api)

Thank you,

Elias 




More information about the Python-list mailing list