[Python-Dev] setting class attributes from C?

Jeremy Hylton jeremy@zope.com
Tue, 05 Mar 2002 00:19:30 -0500


On Tue, 5 Mar 2002 00:08:48 -0500
 "Fred L. Drake, Jr." <fdrake@acm.org> wrote:
> 
> Has anyone tried setting class attributes on a new-style
> class in C?
> The file sandbox/datetime/datetime.py has code that does
> this:
> 
> class datetime(basetime):
>     ...
> 
> datetime.min = datetime(...)
> datetime.max = datetime(...)

Funny you should ask.  Guido and I were just chatting about
this problem this morning.

I think we concluded that the cleanest way to add attributes
to an extension type is to create a subclass in Python:

import _datetime

class datetime(_datetime):
    pass

datetime.min = datetime(...)

You can add attributes to the Python class in Python.

If you really want to do it in C, take a look at Zope3's
Persistence.cPersistence.  After it calls PyType_Ready(), it
adds items to the type's tp_dict slot.

Jeremy