Simple question regarding module initialization

Terry Hancock hancock at anansispaceworks.com
Wed Jun 21 02:31:08 EDT 2006


>  On 6/20/06, Patrick M. Nielsen <thirsteh at gmail.com> wrote:
> > for module in modules: print "Loading %s..." % module exec 'import
> > %s' % module
> >
> > Although I like the outcome of writing it like this, I don't feel
> > good about it. Is this against standard Python conventions or
> > morals? Loading modules this way, that is (instead of just
> > importing it all "the right way" and making a print statement for
> > each one).
> >
> >
Troy Melhase wrote:
>  __import__(name, globals, locals, fromlist) -> module
>
>  Import a module.

... grumble ... top posting ... grumble ... no text ...  grumble ...

(Probably) Troy's point here is that you'd be better off to use the
built-in __import__ function instead of using exec. This is probably
clearer. There is no fundamental reason why it is more secure,
though (the usual complaint about exec), because you are in
complete control of what exec is being fed.

However, I'd still recommend __import__ for clarity.

Cheers,
Terry


-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list