Dynamic class construction?

Tim Peters tim_one at email.msn.com
Mon Jan 31 20:36:34 EST 2000


[Adrian Eyre]
>> [...]
>>
>> There's already a module which does this...
>>
>> import new
>>
>> def anOp(self):
>>         return 'This is an instance of the class %s' % \
>>                 self.__class__.__name__
>>
>> someOps={'__str__':anOp}
>> G=new.classobj('G', (Group,), someOps) # Group defined elsewhere

[David C. Ullrich]
>     I sorta thought there might be a module that does this, but I
> couldn't find it.
>
>     Um, I still can't find it. It appears to be a builtin module -
> new.__doc__ says
>
> '     Functions to create new objects used by the interpreter.
> You need to know a great deal about the interpreter to use this!',
>
> which is helpful, I guess, but I can't find any more about it in
> the docs (or I'm not looking in the right place? It's not in the
> "module index", I don't find any interesting files contaning
> the text "classobj", etc.)

The "new" module is deliberately undocumented.  As the docstring says, you
need to understand the internals of Python inside out to use it safely; but
if you do know the internals sufficiently well to use "new" at all, you
don't need any docs.  The module is not safe for general use (it provides
unchecked access to the internals, and it's very easy to crash the
interpreter by misusing it, and it's impossible not to misuse it if you
don't know the internals ...).

I'm not sure what the problem here is (haven't followed this thread), but
"new" isn't the answer <0.9 wink>.

If that doesn't dissuade you, study Modules/newmodule.c in the source
distribution.

the-code-*is*-the-docs-in-this-case-ly y'rs  - tim






More information about the Python-list mailing list