dynamic class instantiation

Ognen Duzlevski maketo at ukato.freeshell.org
Wed Feb 1 14:04:10 EST 2006


Volker Grabsch <volker_grabsch at v.notjusthosting.com> wrote:
> Ognen Duzlevski wrote:
> > Kent Johnson <kent at kentsjohnson.com> wrote:
> >> Ognen Duzlevski wrote:
> >> > Say I got "page" as a string. How do I go about 
> >> > instantiating a class from this piece of information? To make it 
> >> > more obvious how do I create the page() class based on the "page" 
> >> > string I have? 
> >
> >> Use getattr().
> >
> > Hi Kent, this is exactly what I was looking for. I can't believe I didn't think
> > of getattr() myself! ;(

> However, remember that this solves your problem just temporarily.

> Your main problem is still a deep design failure. Code generators are
> relicts, hard to maintain, and usually just plain ugly. Your application
> isn't that special.

> I'm sure you could replace 2/3 of your code with something much simpler
> (and shorter!) just by not inventing a new language and using the power
> of Python instead.

Hi Volker,

I appreciate your comments. Basically, the reason why this code generator
exists is the fact that I do not want to hard-code the resulting xml in 
any way. The users of the web/db framework of which this solution is part of
might like the "web page" definition I offer them, but, some might want to extend it.
While dom allows me to traverse an xml hierarchy - it does not allow me to
give "meaning" to the tags. The only way to do that (for me) is to represent
the tags as classes with containers containing other classes (tags) and so on. Since
I do not know ahead of time what the tags will be - I need to generate the code
to actually handle them.

Can you suggest a better approach or did you already do that and I just missed 
it? :)

Cheers,
Ognen



More information about the Python-list mailing list