Execution of import (was Re: boolean xor)

Emile van Sebille emile at fenx.com
Mon Jan 8 00:07:16 EST 2001


If you switch it around, does it make a difference?

from operator import truth
def xor(a,b, truth = truth):
    return truth(a) ^ truth(b)

--

Emile van Sebille
emile at fenx.com
-------------------


"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.978928989.19268.python-list at python.org...
> > 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
>
>





More information about the Python-list mailing list