[Tutor] functions in other files, executing

Magnus Lycka magnus@thinkware.se
Thu, 17 Oct 2002 12:41:50 +0200


At 21:24 2002-10-17 +1300, Thomi Richards wrote:
>I am assuming that there is an easier way to execute a function from
>another file, assuming that you know it's location, and the function
>name than importing the file as a module?? (In fact, i think i have seen
>it in one of the modules, but i cannot find it now that i need it).
>
>any ideas?

Get someone else to write the program for you?
Oh boy! Are you so lazy? ;)

What's the problem with importing the file as a
module? Can you explain what kind of problem you
are trying to solve?

Instead of

import witch
whitch.burn()

you can import single names selectively as

from whitch import burn
burn()

This saves some typing if you are going to do a
lot of burning, but is it really important? To
indicate explicitly where a function comes from
helps us understant the code better.

This selective import won't save you any runtime
or memory. burn() might need something else from
the witch module, so it's all loaded into memeory.

If you are bothered by long module names you can do:

import that_witch_module_with_the_long_name as w
w.burn()

The form

from x import y

isn't so horribly bad, but

from x import *

means that you add a lot of variable names to
your local scope, and you can't see where they
came from.

Imagine that you do:

from witch import *
from wood import *

burn()

This works well, since the wood module only
contains the functions measure() and cut().
But then a burn() function is added to the
wood module, and next time you run your program
something unexpected happens... :(



--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se