Importing again and again

Terry Reedy tjreedy at udel.edu
Thu Jun 8 17:31:05 EDT 2006


"John Bokma" <john at castleamber.com> wrote in message 
news:Xns97DCA4EEBEBEEcastleamber at 130.133.1.4...
>> def foo():
>>     import bar
>>     bar.printStuff()
> I am new to Python so this might be a weird question, but it there a
> reason why you import bar inside foo?

Two possible reasons:
1) delay import until actually needed, if ever.
2) put 'bar' into the function local namespace instead of the module global 
namespace

Terry Jan Reedy






More information about the Python-list mailing list