[Tutor] this module

Alan Gauld alan.gauld at btinternet.com
Sun Aug 9 09:29:55 CEST 2009


"Mark Young" <marky1991 at gmail.com> wrote

> Thanks everybody, I didn't know modules ran code when you imported them, 
> I
> just thought they defined the functions, etc. in them.

They do that too.
But in Python a function definition is created by running the code!

So you write

def foo():
     return 42

To define foo you need to execute those two lines of code.
The result is that you create a function object.

So when you import a moduile it runs all of the code in there, and
most of the code usually consists of function and variable definitions.
But it can be anything at all.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list