Modules and Classes - unibo.it

[Pages:33]Modules and Classes

Classes in Python

Module

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module name (as a string) is available as the value of the global variable name . For instance the module math can be imported and used as:

>>> import math >>> dir(math) ['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']

To be able to use pi or e we need to use the suffix module name such as:

>>> math.pi, math.e, math.sqrt(25), math.sin(math.pi/2) (3.1415926535897931, 2.7182818284590451, 5.0, 1.0)

We can change the local name of a module using the statement: import MODULE as NICKNAME, such as:

>>> import math as M >>> M.sqrt(64) 8.0

It is also possible to use the module objects without the suffix name by importing the module objects as: from MODULE import * or explicitly defining the list of the object to be imported as:

from math import log, e

But be careful! Python can override the objects. Look at this code:

e=123.1 # my variable print "before import e=", e from math import log, e print "after import e log(e)",e, log(e) e=199 print "before the assigments of e, log(e)= ", log(e) log=log(55) print "after theassigments of log log(e)=",log(e)

Of course you can write your own modules and use them when needed. For instance, suppose you wrote a file called myregress.py that contains:

def sum(v): ' sum(v) -> sum over the element v[i] ' s=0 for e in v: s=s+e return float(s)

def sum2(v1,v2): ' sum2(v1,v2) -> sum over the v1[i]*v2[i] ' d,s=len(v1),0.0 for i in range(d): s = s + v1[i]*v2[i] return float(s)

def regression(x,y): ''' regression(x,y) computes A and B (y = B x + A) given the lists x and y return values: (A,B) ''' N=len(x) sumX=sum(x) sumXX=sum2(x,x) sumY=sum(y) sumXY=sum2(x,y) D=N*sumXX - (sumX)**2.0 A=(sumXX*sumY-sumX*sumXY)/D B=(N*sumXY-sumX*sumY)/D return A,B

You now can use the functions importing the module as:

>>> import myregress >>> dir(myregress) ['__builtins__', '__doc__', '__file__', '__name__', 'regression', 'sum', 'sum2'] >>> print myregress.regression.__doc__ # print the regression info

regression(x,y) computes A and B (y = B x + A) given the lists x and y return values: (A,B)

>>> x=[1,2,3,4] # make a list for x

>>> y=[2,4,6,8] # make a list for y

>>> myregress.regression(x,y) # compute A and B

(0.0, 2.0)

>>> y2=[2,4.5,5.5,8]

# another y

>>> myregress.regression(x,y2) # another A,B

(0.25, 1.8999999999999999)

>>>

You could have also adopted the alternative way:

>>> from myregress import regression >>> x=[1,2,3,4] >>> y=[2,4,6,8] >>> regression(x,y) (0.0, 2.0)

As you may remember the string just after the function definition is the documentation string.

>>> from myregress import regression >>> >>> print regression.__doc__

regression(x,y) computes A and B (y = B x + A) given the lists x and y return values: (A,B)

>>> print sum2.__doc__ Traceback (most recent call last):

File "", line 1, in ? NameError: name 'sum2' is not defined >>>

Classes and Objects

Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes are created at runtime, and can be modified further after creation.

Classes and Objects

As an example, we build the DNA class. So given the DNA object dna1 it can be characterized by the following attributes:

name sequence length

dna1

name seq len

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download