PINOY BLAST
WELCOME TO
PINOY BLAST COMMUNITY!!
PINOYBLAST CLAN MEMBER TAG:
SITE: PB|NICK
UZZAP: +PB|^NICK

Join the forum, it's quick and easy

PINOY BLAST
WELCOME TO
PINOY BLAST COMMUNITY!!
PINOYBLAST CLAN MEMBER TAG:
SITE: PB|NICK
UZZAP: +PB|^NICK
PINOY BLAST
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 


Rechercher Advanced Search

Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest

None

Most users ever online was 14 on Wed Apr 23, 2014 10:25 am
RSS feeds


Yahoo! 
MSN 
AOL 
Netvibes 
Bloglines 


Social bookmarking

Social bookmarking reddit      

Bookmark and share the address of KiLL ALL TRIBE.....EXCEPT BaTan6TRiBE on your social bookmarking website

Bookmark and share the address of PINOY BLAST on your social bookmarking website


MODULE PYSCRIPT TUTORIAL........

Go down

MODULE PYSCRIPT TUTORIAL........ Empty MODULE PYSCRIPT TUTORIAL........

Post by AdminExdee Tue May 03, 2011 9:11 am

Module? What's a Module?


MODULETEST.py



### EXAMPLE PYTHON MODULE
# Define some variables:
numberone = 1
ageofqueen = 78

# define some functions
def printhello():
print "hello"

def timesfour(input):
print input * 4

# define a class
class Piano:
def __init__(self):
self.type = raw_input("What type of piano? ")
self.height = raw_input("What height (in feet)? ")
self.price = raw_input("How much did it cost? ")
self.age = raw_input("How old is it (in years)? ")

def printdetails(self):
print "This piano is a/an " self.height " foot",
print self.type, "piano, " self.age, "years old and costing\
" self.price " dollars."
[color=rgb(0, 255, 255)]As you see, a module looks pretty much like your normal python program.

So what do we do with a module? We import bits of it (or all of it) into other programs.
To import all the variables, functions and classes from moduletest.py
into another program you are writing, we use the import operator. For
example, to import moduletest.py into your main program, you would have
this:


Mainprogram.py

### mainprogam.py
### IMPORTS ANOTHER MODULE
import moduletest
This assumes that the module is in the same directory as mainprogram.py,
or is a default module that comes with python. You leave out the '.py'
at the end of the file - it is ignored. You normally put all import
statements at the beginning of the python file, but technically they can
be anywhere. In order to use the items in the module in your main
program, you use the following:


Mainprogram.py continued

### USING AN IMPORTED MODULE
# Use the form modulename.itemname
# Examples:
print moduletest.ageofqueen
cfcpiano = moduletest.Piano()
cfcpiano.printdetails()

As you see, the modules that you import act very much like the
classes we looked at last lesson - anything inside them must be
preceeded with modulename. for it to work.


More module thingummyjigs (in lack of a better title)
[


Wish you could get rid of the modulename. part that you have to put before every item you use from a module? No? Never? Well, I'll teach it you anyway.

One way to avoid this hassle is to import only the wanted objects from the module. To do this, you use the from operator. You use it in the form of from modulename import itemname. Here is an example:

[center]Code Example 4 - importing individual objects

### IMPORT ITEMS DIRECTLY INTO YOUR PROGRAM

# import them
from moduletest import ageofqueen
from moduletest import printhello

# now try using them
print ageofqueen
printhello()

What is the point of this? Well, maybe you could use it to make
your code a little more readable. If we get into heaps of modules
inside modules, it could also remove that extra layer of crypticness.


If you wanted to, you could import everything from a module is this way by using from modulename import *.
Of course, this can be troublesome if there are objects in your program
with the same name as some items in the module. With large modules,
this can easily happen, and can cause many a headache. A better way to
do this would be to import a module in the normal way (without the from
operator) and then assign items to a local name:



mainprogram.py continued
### ASSIGNING ITEMS TO A LOCAL NAME

# Assigning to a local name
timesfour = moduletest.timesfour

# Using the local name
print timesfour(565)


This way, you can remove some crypticness, AND have all of the items from a certain module.

Conclusion

That's it! A very simple lesson, but now you can organise your
programs very neatly. In fact, now it is increadibly easy to make
progams that can grow in complexity without ending up with one cryptic
file that is full of bugs.
AdminExdee
AdminExdee
Administrator
Administrator

Posts : 204
PINOY BLAST Coins : 66632
Join date : 22/10/2010
Age : 29
Location : BATANGAS

http://rk404notfound.nstars.org

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum