[Tutor] Dynamically composing a module name

Alan Gauld alan.gauld at freenet.co.uk
Thu Apr 21 23:04:03 CEST 2005


>   Inside of the setup() function, a call to the cgi object
determines
>   the name of the calling application (form1,py ... etc.) and
>   then imports another module (automatically generated from another
>   process), always having the same members, composing that auto
module
>   name from the name of the calling application:  form1.py =>
>   form1_extra.py......  And if I have to change the algorithm that
>   determines the name of the module imported or the names of the
>   members of the modules, I could just do it in two places:
>   the setup() function and the application generating the auto
modules
>   (which isn't necessarily written in python).

This sounds to me like a description of an object oriented design
using inheritance and polymorphism. An abstract class that defines
a standard set of methods that are called in a generic way which
may be substituted at runtime by any one of a number of subclasses
each implementing their own specialised version of the generic
behaviour.

The snag with importing at point of use is that importing a newly
created module is one of the slowest things you an do in Python - it
has to compile it and then read it and then execute it! If you
can load the moules in advance somehow (maybe in a thread?) there
might be significant gains...

Alan G.



More information about the Tutor mailing list