Re-executing the code object from a class 'declaration'
Alex Martelli
aleaxit at yahoo.com
Wed Oct 6 12:58:40 EDT 2004
Carlos Ribeiro <carribeiro at gmail.com> wrote:
> On 5 Oct 2004 23:33:01 -0700, Michele Simionato
> <michele.simionato at gmail.com> wrote:
> > which does something close to what you ask for. Of course the trick
> > there is NOT recommended for real code ;)
>
> That was the best advice I could have :-) Really. I'm trying to outwit
> Python, and one has to have the good sense to stop doing it at some
> point. I'm giving up (unless I found a really great way to make it),
> and I'll use this idiom instead:
>
> def BuildTemplate(<template parameters>)
> class TemplateClass(...):
> ...
> return TemplateClass
>
> It's simple, and less hackish, but still unusual. I fear I may be
> missing something on my design, if I need to resort to such hacks.
Personally, I _like_ using 'class' statements to make classes and 'def'
statements to make functions; I don't see either as hacks. Messing with
bytecodes, building a function with new.function around a certain code
object, calling the metaclass explicitly... that feels more hackish to
me, personally. I do it when I must, but 99.9% of the time I have no
problem avoiding it in production code, it seems to me.
Alex
More information about the Python-list
mailing list