[Tutor] how import a module upon instantiation of a class?

Peter Otten __peter__ at web.de
Mon Sep 1 01:17:52 CEST 2014


Albert-Jan Roskam wrote:

> I want to import a module upon instantiation (not definition) of a class.
> What is the best way to do this? In my case, I need the "icu" module in
> only one place/class of the program. If that class won't be used, I don't
> want to have an ImportError. Also, it might be nice to do the imports only
> when you actually need that functionality. It's probably not really
> PEP-compliant to put the imports somewhere else than at the top of the
> module, but I can live with that.
> 
> 
> import some_nonbuiltin  # I don't want it here!
> 
> 
> class KlassA(object):
>     import some_nonbuiltin  # nope
> 
>     def __init__(self):
          global some_nonbuiltin
          import some_nonbuiltin



More information about the Tutor mailing list