Generic Python

John Roth johnroth at ameritech.net
Mon Jun 24 07:36:42 EDT 2002


"Uwe Mayer" <Uwe.Mayer at ifib.uni-karlsruhe.de> wrote in message
news:3D16E465.F0D2509E at ifib.uni-karlsruhe.de...
> Hi,
>
> is it possible to to write a class which, f.e. takes an argument in
> __init__() and that doesn't return an instance object but a new class
> object?

Sure, but you have to do it closer to the source level. One
quibble here: you can't do it with the __init__() method, that's
defined to always return an instance. You have to do it with some
kind of generator or factory function.

The way you do it is to create the source in a string, and
then use exec.

However, as another poster mentions, there may be better
ways of solving your specific problem. Dynamically generating
pieces of program is useful in some highly specialized
circumstances, but this might not be one of them.

John Roth






More information about the Python-list mailing list