[capi-sig] Creating type object dynamically in run-time

Mateusz Loskot mateusz at loskot.net
Thu May 10 13:47:02 CEST 2012

On 10 May 2012 12:28, Stefan Behnel <python_capi at behnel.de> wrote:
> Mateusz Loskot, 10.05.2012 13:24:
>> This is my first post to this list, hello everyone!
>> I hope this post is not off-topic here.
>> I'm writing fairly complex structure of Python extensions directly using
>> Python C API (version 3+ only). I define some type objects with statically,
>> according to the canonical Noddy example presented in the docs.
>> Some type objects have to be defined/composed dynamically in run-time.
> Do you really need a type or is a Python class enough? The letter would be
> much easier to create.


I'm not sure I understand.
Aren't terms "class" and "type" names of the same concept, since Python 2.2?
What is the noddy_NoddyType in the "Defining New Types" example?
I'm sorry if I'm lacking of basics, but I follow the terminology as explained by
Eli Bendersky in

In my system, I'm embedding Python and I add custom Python extension
too (let's call it 'emb')
So, users of my embedded Python have access to 'emb' module.
The 'emb' module defines number of types, some are defined statically,
as the noddy_NoddyType, so users can instantiate it

n = emb.Noddy()
n.bar() # method defined statically in methods table of noddy_NoddyType

Now, I'd like to add some types which are generated in run-time, way
before the 'emb' module is appended to inittab and embedded Python is
And, I'd like to enable users to instantiate them in the same way as
Noddy above:

d = emb.GeneratedNoddy()

or allow users to use and access

d  = emb.foo()
d.bar() # added dynamically in run-time during emb.GeneratedNoddy composition


<class 'emb.GeneratedNoddy'>

I hope it makes my intentions clear.

Best regards,
Mateusz Loskot, http://mateusz.loskot.net

More information about the capi-sig mailing list