Loading just in time

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Jul 11 20:19:20 EDT 2008


Ross Ridge <rridge at csclub.uwaterloo.ca> writes:

> D'Arcy J.M. Cain <darcy at druid.net> wrote:
> >def calc_tax(*arg, **name):
> >    from calc_tax import calc_tax as _func_
> >    calc_tax = _func_
> >    return _func_(*arg, **name)
> 
> This should do what you want:
> 
> 	def calc_tax(*arg, **name):
> 		global calc_tax
> 		from calc_tax import calc_tax
> 		return calc_tax(*arg, **name)
> 
> I suspect though that the cost of importing a lot of little modules
> won't be as bad as you might think.

The performance cost isn't the only consideration. Importing modules
at arbitrary places scattered through the code imposes a maintenance
burden on any reader of the code.

For this reason, PEP 8 <URL:http://www.python.org/dev/peps/pep-0008>
requires conformant code to have imports in a standard location in the
module:

    - Imports are always put at the top of the file, just after any module
      comments and docstrings, and before module globals and constants.

      Imports should be grouped in the following order:

      1. standard library imports
      2. related third party imports
      3. local application/library specific imports

      You should put a blank line between each group of imports.

      Put any relevant __all__ specification after the imports.

This doesn't contradict the use of many small modules, but please,
keep them imported according to PEP 8.

-- 
 \      “People always ask me, ‘Where were you when Kennedy was shot?’ |
  `\                        Well, I don't have an alibi.” —Emo Philips |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list