Mass importing of a template based system.. Trouble with name substitutions
rh0dium
sklass at pointcircle.com
Thu Aug 4 11:50:55 EDT 2005
Peter Otten wrote:
> rh0dium wrote:
>
> > for mod in modules:
> > a = mod.mod()
> > a.run()
>
> Puzzle: If mod.mod did what you expect, what would a.run 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
example1.py contains a class example1 and a module run
example2.py contains a class example2 and a module run
example3.py 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__)()
> a.run()
>
> Peter
More information about the Python-list
mailing list