HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Creating and Importing a Module

#############################

Migrated to http://pythonadventures.wordpress.com/2010/09/30/create-import-module/.

If you have some functions that you use often, you can collect them in a module.

mymodule.py:

def f1(n):
    return n + 1

use-mymodule.py:

#!/usr/bin/env python

import mymodule

print mymodule.f1(5)      # => 6
print mymodule.__name__   # => mymodule (note that .py is missing)

It is also possible to add some testing code to a module:

mymodule.py:

#!/usr/bin/env python

def f1(n):
    return n + 1

if __name__ == "__main__":
    number = 1977
    print f1(number)   # => 1978

Now, you can still import it like in use-mymodule.py, or you can launch it as if it were a standalone script. In the latter case the test suite will be executed. If you import it, the test suite is not executed. A test suite is a good practice to be sure that the module is working as expected.

Cloud City


anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows


Blogs and Dev.

* Ubuntu Incident
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2010 September 30, 15:58