Execution of import (was Re: boolean xor)

Delaney, Timothy tdelaney at avaya.com
Sun Jan 7 23:44:55 EST 2001


I think the real question here though is ... how much overhead is there in
calling

	from operator import truch

inside the function each time through (other than the first). There is some
overhead in determining that the module is already imported (even though it
should only be a dictionary lookup).

Tim Delaney
Avaya Australia
+61 2 9352 9079

> -----Original Message-----
> From: Tim Peters [mailto:tim.one at home.com]
> Sent: Monday, 8 January 2001 3:43 PM
> To: python-list at python.org
> Subject: RE: Execution of import (was Re: boolean xor)
> 
> 
> > def xor(a,b):
> >     from operator import truth
> >     return truth(a) ^ truth(b)
> 
> [Peter Hansen]
> > An aside: does the 'from .. import' statement  execute every time
> > the function 'xor' is called, or only during the execution
> > of the 'def' statement?
> 
> Every stmt in Python is executable except for "global", and 
> every executable
> stmt executes where it looks like it should <wink>.  "from" 
> is the same as
> "x=1" in those respects (for that matter, so are "def" and 
> "class").  So,
> yes, every time 'xor' is called.  But imported modules are cached in
> sys.modules, so anything that imports module X will cause X to get
> *executed* only if it's the first time X is imported (by 
> anything) during
> the program run.  Imports of X after the first are just 
> lookups, mapping the
> module name to the (already executed) module object and retrieving the
> latter.
> 
> blazingly-fast-ly y'rs  - tim
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list