Mateusz Loskot, 10.05.2012 13:47:
> On 10 May 2012 12:28, Stefan Behnel 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?
With "type" I meant "extension type", i.e. the one you define using C
structs. The alternative would be a simple Python class as you would get with
class MyClass(object): pass
> What is the noddy_NoddyType in the "Defining New Types" example?
That's an extension type.
> 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
Why would you want to do that before initialising the Python runtime?
> 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.
Not clear enough. The question is what your dynamically created types
should do and provide. Would they need to be implemented in C (not just
their methods but the types themselves!) or would a Python implementation
suffice, potentially with methods implemented in C?
You may get away with using normal Python classes that inherit from an
extension type, for example. In that case, I'd also advise you to take a
look at Cython, because that makes both the implementation of extension
types and the creation of Python classes inheriting from them as easy as Py.