having a function in a separate file

Terry Reedy tjreedy at udel.edu
Sun Oct 5 05:49:35 CEST 2008


Maryam Saeedi wrote:
> Hi all,
> 
> I have a very basic question. I was wondering if I can have a function 
> in a separate file

Yes, the separate file become a module.

> and in my main file sort of import that function and 

Yes.  either of

import somemod # and use somemod.func

from somemod import func # and use func

> if so are all the variables local

The function can access built-in objects and objects in the module it is 
defined in.  Otherwise, it has locals just as usual.

> and if I need any variables in the  function I should give it as an input?

Yes.

> I need to call a function in many different tasks and I am improving 
> that function so I prefer to make the changes only once. If you have any 
> example I really appreciate it.

Look at Python coded files in the the standard library.  Look in 
.../Pythonx.y/Lib to find them.

tjr




More information about the Python-list mailing list