Mass importing of a template based system.. Trouble with name substitutions

rh0dium sklass at
Thu Aug 4 17:50:55 CEST 2005

Peter Otten wrote:
> rh0dium wrote:
> > for mod in modules:
> >     a = mod.mod()
> >
> Puzzle: If mod.mod did what you expect, what would have to do to
> maintain consistency?

I thought that once a = example.example the class is then loaded.
Since my framework defines a python file with a class named the same
within it.  So contains a class example1 and a module run contains a class example2 and a module run contains a class example3 and a module run

Additional methods that must be present within the class include a run
method.  Since this was by definition I knew I needed to do a
substitution - namely mod.mod ( which was intended to be translated to
example1.example1 ).

What you provided was very slick indeed.  I no longer am dependant upon
the naming to get the job done.

Very nice - How does this work if some fool calls one class the same as
the other.  I'm assuming the last class will be loaded - not the first.

> There would be no way to determine the name of the module bound to the mod
> variable, but fortunately the Python developers foresaw your problem and
> stashed it (the name) in the __name__ attribute.
> Use getattr(obj, attrname) to look up an attribute whose name is not known
> at compile time:

OUTSTANDING!! Thanks so much!

> for mod in modules:
>     a = getattr(mod, mod.__name__)()
> Peter

More information about the Python-list mailing list