[Tutor] use my own code

Alan Gauld alan.gauld at btinternet.com
Mon Nov 26 01:15:36 CET 2007

"elis aeris" <hunter92383 at gmail.com> wrote 

> ok, i can import them.
> how do I use them though?

When you import the module the code is executed. 
If you have executable code rather than function or 
class definitions then it will be executed (print 
statements etc). Thats why its always good practice 
to wrap stuff in functions and then use an

if __name__ == "__main__":

Type construct to execute it, it makres reuse later 
much easier.

But to access the functions and variables you just 
call them with the module prefix as usual

import sys, mymodule


See? exactly the same as you would use sys...

There's a lot more on this in the Modules and Functions 
topic in my tutorial.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list