Python C api: create a new object class

samwyse samwyse at gmail.com
Wed Nov 11 08:26:01 EST 2009


On Nov 10, 1:09 pm, "lallous" <lall... at lgwm.org> 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



More information about the Python-list mailing list