Python C api: create a new object class

Discussion in 'Python' started by lallous, Nov 10, 2009.

  1. lallous

    lallous Guest

    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
    lallous, Nov 10, 2009
    #1
    1. Advertising

  2. > 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
    Martin v. Löwis, Nov 10, 2009
    #2
    1. Advertising

  3. 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.
    Benjamin Peterson, Nov 10, 2009
    #3
  4. lallous

    samwyse Guest

    On Nov 10, 1:09 pm, "lallous" <> 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
    samwyse, Nov 11, 2009
    #4
    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. E11
    Replies:
    1
    Views:
    4,737
    Thomas Weidenfeller
    Oct 12, 2005
  2. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    372
    Praveen, Tayal (IE10)
    Mar 17, 2005
  3. jon wayne
    Replies:
    9
    Views:
    705
    Jim Langston
    Sep 22, 2005
  4. Paul Childs
    Replies:
    1
    Views:
    320
    Matimus
    Jun 3, 2008
  5. Krithika San
    Replies:
    3
    Views:
    333
    Marnen Laibow-Koser
    Nov 14, 2009
Loading...

Share This Page