Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python C api: create a new object class

Reply
Thread Tools

Python C api: create a new object class

 
 
lallous
Guest
Posts: n/a
 
      11-10-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

 
Reply With Quote
 
 
 
 
Martin v. L÷wis
Guest
Posts: n/a
 
      11-10-2009
> 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', (), {})


Most certainly:

o = empty(1) # or: o = empty(1, etc)

This requires you to define

class empty:
def __init__(self, myattr, etc):
self.myattr = myattr
etc

> 2)
>
> How can I, similarly, create an object "o" in C api:
>
> PyObject *o = what_to_call(....)


o = PyObject_CallFunction(pointer_to_class_object, "")

> 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)


You could check whether the object supports setattr at all, but that
would be pretty useless, since most objects do.

What you want to test (would it support setting "myattr" to the specific
value, at this point) is impossible to test: the object may give you
an exception on every third call only (or only if the value is not
an integer, etc). So just call SetAttr, and clear any exception you
get that you don't want to get.

Regards,
Martin
 
Reply With Quote
 
 
 
 
Benjamin Peterson
Guest
Posts: n/a
 
      11-10-2009
lallous <lallous <at> lgwm.org> writes:
> Is there is a one line syntax to instantiate an instance?


You can't instantiate an instance; it's already instantiated.

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


class x: pass

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


Use PyObject_CallFunction(PyType_Type, [arguments])


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


No.




 
Reply With Quote
 
samwyse
Guest
Posts: n/a
 
      11-11-2009
On Nov 10, 1:09*pm, "lallous" <(E-Mail Removed)> wrote:
> 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?


I think that you want this:

class c(object):
def __init__(self, **kwds):
self.__dict__ = kwds

x = c(a=1, b=2)
print x.a, x.b
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to create the pointer object of a class at run-time (gcc weaklinking of object) rippul C++ 7 11-28-2010 06:39 PM
Creating object in function doesn't seem to create a new object. Paul Childs Python 1 06-03-2008 08:20 PM
How to create an object of a partial class user control in a seperate public class? MurdockSE ASP .Net Web Controls 0 09-13-2006 04:38 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments