refactoring a group of import statements

rantingrick rantingrick at gmail.com
Sun Jun 27 20:02:50 EDT 2010


On Jun 27, 6:09 pm, Thomas Jollans <tho... at jollans.com> wrote:

> > import Tkinter as tk
> > try:
> >     import Image #from PIL
> >     print 'Using high quality images :)'
> > except ImportError:
> >     print 'Using low quality images :('
>
> As such, that still appears rather useless - the following code doesn't
> know how to behave ;-) Take this example from my own code: ;-)

Ah yes that was me trying to save a little server space by not posting
my whole script. So Yes I did leave a little "mystery" for the
advanced reader to dissect. *wink*

> Yes, sometimes these checks are needed, because a module can enhance
> your code if it's present. But that tends to be the exception rather
> than the rule, and would usually require some extra code to make the
> check useful in the first place, which makes a generalized for loop over
> a bunch of modules appear to be of little utility

agreed! However i was going to let the OP find this out though his own
realization and in the meantime show him the power of eval/exec even
if it could blow a pinky toe off ;-).

> Ah yes, exec. I've never liked exec - I find
> globals()[x] = __import__(x)
> clearer. But that might be just me.

What's the irrational fear/hatred some have of eval and exec? You *do*
know that harnessing these facilities simply invokes an underlying
process that the interpretor itself uses every time you import or run
a script. So if you fear eval/exec you *really* should fear the Python
interpretor *gasp*! Does that seem logical? Really don't fear eval/
exec! No, fear the while loop as it's much more apt to breaking loose
and running amuck!





More information about the Python-list mailing list