Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Introspection on Python

In this part of the Python programming tutorial, we will talk about introspection.
Introspection is an act of self examination. In computer programming, introspection is the ability to determine the type of an object at runtime. Python programming language has a large support of introspection. Everything in Python is an object. Every object in Python may have attributes and methods. By using introspection, we can dynamically inspect Python objects.

The dir() function

The dir() function is the most important function when doing introspection. The function returns a sorted list of attributes and methods belonging to an object.
>>> dir(())
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'count', 'index']
Here we see an output of the dir() function for a tuple object.
>>> print ().__doc__
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items

If the argument is a tuple, the return value is the same object.
Our investigation showed, that there is a __doc__ attribute for a tuple object.
#!/usr/bin/python

# dir.py

import sys

class Object:
def __init__(self):
pass
def examine(self):
print self


o = Object()

print dir(o)
print dir([])
print dir({})
print dir(1)
print dir()
print dir(len)
print dir(sys)
print dir("String")
The example examines several objects using the dir() function. A user defined object, native data types, a function, a string or a number.

Without any argument, the dir() returns names in the current scope.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> import sys
>>> import math, os
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'math', 'os', 'sys']
We execute the dir() function before and after we include some modules.

The type(), id() functions

The type() function returns the type of an object.
#!/usr/bin/python

# types.py

import sys


def function(): pass

class MyObject():
def __init__(self):
pass

o = MyObject()

print type(1)
print type("")
print type([])
print type({})
print type(())
print type(object)
print type(function)
print type(MyObject)
print type(o)
print type(sys)
The example print various types of objects to the console screen.
$ ./types.py 
<type 'int'>
<type 'str'>
<type 'list'>
<type 'dict'>
<type 'tuple'>
<type 'type'>
<type 'function'>
<type 'classobj'>
<type 'instance'>
<type 'module'>
Output of the types.py script.

The id() returns a special id of an object.
#!/usr/bin/python

# ids.py

import sys

def fun(): pass

class MyObject():
def __init__(self):
pass

o = MyObject()

print id(1)
print id("")
print id({})
print id([])
print id(sys)
print id(fun)
print id(MyObject)
print id(o)
print id(object)
The code example prints ids of various objects. Both built-in and custom.
$ ./ids.py 
135717024
3084304536
3084111220
3084104940
3084304500
3084112812
3084074556
3084130444
135568640

The sys module

The sys module provides access to system specific variables and functions used or maintained by the interpreter and to functions that interact strongly with the interpreter. The module allows us to query about the Python environment.
>>> import sys
>>> sys.version
'2.7.2+ (default, Oct 4 2011, 20:03:08) \n[GCC 4.6.1]'
>>> sys.platform
'linux2'
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
In the above code we examine the Python version, platform and search path locations.

We will have another four variables of the sys module. We can use the dir() function to get a full list of variables and functions of the sys module.
>>> sys.maxint
2147483647
>>> sys.executable
'/usr/bin/python'
>>> sys.argv
['']
>>> sys.byteorder
'little'
The example presents maxint, executable, argv and byteorder attributes of the sys module.
>>> sys.maxint
2147483647
The maxint is the largest positive integer supported by Python's regular integer type.
>>> sys.executable
'/usr/bin/python'
The executable is a string giving the name of the executable binary for the Python interpreter, on systems where this makes sense.
>>> sys.argv
['']
This gives a list of command line arguments passed to a Python script.
>>> sys.byteorder
'little'
The byteorder is an indicator of the native byte order. This will have the value 'big' on big-endian (most-significant byte first) platforms, and 'little' on little-endian (least-significant byte first) platforms.

Various

Next we will show various other ways of inspecting our objects.
#!/usr/bin/python

# attr.py

def fun(): pass


print hasattr(object, '__doc__')
print hasattr(fun, '__doc__')
print hasattr(fun, '__call__')

print getattr(object, '__doc__')
print getattr(fun, '__doc__')
The hasattr() function checks, if an object has an attribute. The getattr() function returns the contents of an attribute, if there are some.
$ ./attr.py 
True
True
True
The most base type
None
The isinstance function checks, if an objects is an instance of a specific class.
>>> print isinstance.__doc__
isinstance(object, class-or-type-or-tuple) -> bool

Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
We can get the describtion of a function interactively.
#!/usr/bin/python

# instance.py


class MyObject():
def __init__(self):
pass

o = MyObject()

print isinstance(o, MyObject)
print isinstance(o, object)
print isinstance(2, int)
print isinstance('str', str)
As we know, everything is an object in Python. Even numbers or strings. The object is a base type of all objects in Python. That is why isinstance(o, object) returns True.
$ ./instance.py 
True
True
True
True
The issubclass() function checks, if a specific class is a derived class of another class.
#!/usr/bin/python

# subclass.py

class Object():
def __init__(self):
pass

class Wall(Object):
def __init__(self):
pass

print issubclass(Object, Object)
print issubclass(Object, Wall)
print issubclass(Wall, Object)
print issubclass(Wall, Wall)
In our code example, the Wall class is a subclass of the Object class. Interestingly, Object and Wall are subclasses of themselves. The Object class is not a subclass of class Wall.
$ ./subclass.py 
True
False
True
True
The __doc__ attribute gives some documentation about an object and the __name__ attribute holds the name of the object.
#!/usr/bin/python

# namedoc.py

def noaction():
'''A function, which does nothing'''
pass


funcs = [noaction, len, str]

for i in funcs:
print i.__name__
print i.__doc__
print "-" * 75
In our example, we crete a list of three functions. One custom and two native. We go through the list and print the __name__ and the __doc__ attributes.
$ ./namedoc.py
noaction
A function, which does nothing
---------------------------------------------------------------------------
len
len(object) -> integer

Return the number of items of a sequence or mapping.
---------------------------------------------------------------------------
str
str(object) -> string

Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
---------------------------------------------------------------------------
Output.
Finally, there is also a callable() function. The function checks, if an object is a callable object. Or in other words, if an object is a function.
#!/usr/bin/python

# callable.py

class Car:

def setName(self, name):
self.name = name

def fun():
pass

c = Car()

print callable(fun)
print callable(c.setName)
print callable([])
print callable(1)
In the code example we check if three objects are callables.
print callable(fun)
print callable(c.setName)
The fun() function and the setName() method are callables. (A method is a function bound to an object.)
$ ./callable.py
True
True
False
False
In this part of the Python tutorial, we have talked about introspection in Python. More tools for doing introspection can be found in the inspect module.
Continue Reading

Iterators and Generators in Python

According to Wikipedia, an iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation.
In Python programming language, an iterator is an object which implements the iterator protocol. The iterator protocol consists of two methods. The __iter__() method, which must return the iterator object and the next() method, which returns the next element from a sequence.
Python has several built-in objects, which implement the iterator protocol. For example lists, tuples, strings, dictionaries or files.
#!/usr/bin/python

# iter.py


str = "formidable"

for i in str:
print i,

print

it = iter(str)

print it.next()
print it.next()
print it.next()

print list(it)
In the code example, we show a built-in iterator on a string. In Python a string is an immutable sequence of characters. The iter() function returns an iterator on object. We can also use the list()or tuple() functions on iterators.
$ ./iter.py 
f o r m i d a b l e
f
o
r
['m', 'i', 'd', 'a', 'b', 'l', 'e']
Iterators have several advantages:
  • Cleaner code
  • Iterators can work with infinite sequences
  • Iterators save resources
By saving system resources we mean, that when working with iterators, we can get the next element in a sequence without keeping the entire dataset in memory.
#!/usr/bin/python

# wantme1.py

f = open('ifyouwantme', 'r')

while True:
line = f.readline()
if not line: break
else: print line,

f.close()
This code prints the contents of the ifyouwantme file.
#!/usr/bin/python

# wantme2.py

f = open('ifyouwantme', 'r')

for line in f:
print line,

f.close()
The wantme2.py script does the same. In this case, we use iterators. The code is cleaner.
In the following example, we create our own object that will implement the iterator protocol.
#!/usr/bin/python

# iterator.py


class seq:
def __init__(self):
self.x = 0

def next(self):
self.x += 1
return self.x**self.x

def __iter__(self):
return self


s = seq()
n = 0

for i in s:
print i
n += 1
if n > 10:
break
In the code example, we create a sequence of numbers 1, 4, 27, 256, ... . This demonstrates that with iterators, we can work with infinite sequences. The for statement calls the iter() function on the container object. The function returns an iterator object that defines the method next() which accesses elements in the container one at a time.
 def next(self):
self.x += 1
return self.x**self.x
The next() method returns the next element of a sequence.
 def __iter__(self):
return self
The __iter__ method returns the iterator object.
 if n > 10:
break
Because we are working with an infinite sequence, we must interrupt the for loop.
$ ./iterator.py 
1
4
27
256
3125
46656
823543
16777216
387420489
10000000000
285311670611
The loop can be interupted in another way. In the class definition we must raise a StopIteration exception. In the following example, we redo our previous example.
#!/usr/bin/python

# stopiter.py


class seq14:
def __init__(self):
self.x = 0

def next(self):
self.x += 1
if self.x > 14:
raise StopIteration
return self.x**self.x

def __iter__(self):
return self


s = seq14()

for i in s:
print i
The code example will print first 14 numbers of a sequence.
if self.x > 14:
raise StopIteration
The StopIteration exception will cease the for loop.

Generators

In general, a generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is similar to a function returning an array. A generator has parameters, it can be called and it generates a sequence of numbers. But unlike functions, which return a whole array, a generator yields one value at a time. This requires less memory. (Wikipedia)
Generators in Python:
  • Are defined with the def keyword
  • Use the yield keyword
  • May use several yield keywords
  • Return an iterator
Let's look at an generator example.
#!/usr/bin/python

# generator.py


def gen():
x, y = 1, 2
yield x, y
x += 1
yield x, y


it = gen()

print it.next()
print it.next()

try:
print it.next()
except StopIteration:
print "Iteration finished"
As we can see, a generator is defined with a def keyword, just like normal functions. We use two yield keywords inside the body of a generator. Now it is important to understand, how actually the yield keyword works. It exits the generator and returns a value. Next time the next()function of an iterator is called, we continue on the line following the yield keyword. Note that the local variables are preserved throughout the iterations. When there is nothig left to yield, a StopIteration exception is raised.
$ ./generator.py 
(1, 2)
(2, 2)
Iteration finished
The following example we will calculate fibonacci numbers. The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself.
#!/usr/bin/python

# fibonacci.py

import time
import sys

def fib():
a, b = 0, 1
while True:
yield b
a, b = b, a + b


iter = fib()

try:
for i in iter:
print i,
time.sleep(1)
sys.stdout.flush()
except KeyboardInterrupt:
print "Calculation stopped"
The script will continuously print fibonacci numbers to the console.
In this chapter, we have covered iterators and generators in Python.
Continue Reading

Exceptions in Python

In this part of the Python programming tutorial, we will talk about exceptions in Python.
Errors detected during execution are called exceptions. During the execution of our application, many things might go wrong. A disk might get full and we cannot save our file. A internet connection might go down and our application tries to connect to a site. All these might result in a crash of our application. To prevent happening this, we must cope with all possible errors that might occur. For this, we can use the exception handling.
>>> 3 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
It is not possible to divide by zero. If we try to do this, a ZeroDivisionError is raised and the script is interrupted.
$ cat zerodivision.py 
#!/usr/bin/python

# zerodivision.py


def input_numbers():
a = float(raw_input("Enter first number:"))
b = float(raw_input("Enter second number:"))
return a, b

x, y = input_numbers()
print "%d / %d is %f" % (x, y, x/y)
In this script, we get two numbers from the console. We divide these two numbers. If the second number is zero, we get an exception.
$ ./zerodivision.py 
Enter first number:2
Enter second number:0
Traceback (most recent call last):
File "./zerodivision.py", line 12, in <module>
print "%d / %d is %f" % (x, y, x/y)
ZeroDivisionError: float division
We could handle this in two ways.
#!/usr/bin/python

# zerodivision2.py


def input_numbers():
a = float(raw_input("Enter first number:"))
b = float(raw_input("Enter second number:"))
return a, b


x, y = input_numbers()

while True:
if y != 0:
print "%d / %d is %f" % (x, y, x/y)
break
else:
print "Cannot divide by zero"
x, y = input_numbers()
First, we simply check that y value is not zero. If the y value is zero, we print a warning message and repeat the input cycle again. This way, we handled the error and the script is not interrupted.
$ ./zerodivision2.py 
Enter first number:4
Enter second number:0
Cannot divide by zero
Enter first number:5
Enter second number:0
Cannot divide by zero
Enter first number:5
Enter second number:6
5 / 6 is 0.833333
The other way is to use exceptions.
#!/usr/bin/python

# zerodivision3.py


def input_numbers():
a = float(raw_input("Enter first number:"))
b = float(raw_input("Enter second number:"))
return a, b


x, y = input_numbers()

while True:
try:
print "%d / %d is %f" % (x, y, x/y)
break
except ZeroDivisionError:
print "Cannot divide by zero"
x, y = input_numbers()
After try keyword, we put the code, where we expect an exception. The except keyword catches the exception, if it is raised. We specify, what kind of exception we are looking for.

except ValueError:
pass
except (IOError, OSError):
pass
To handle more exceptions, we can either use more except keywords or place the exception names inside a tuple.

Second argument of the except keyword

If we provide a second argument for the except keyword, we get a reference to the exception object.
#!/usr/bin/python

# zero.py

try:
3/0
except ZeroDivisionError, e:
print "Cannot divide by zero"
print "Message:", e.message
print "Class:", e.__class__
From the exception object, we can get the error message or the class name.
$ ./zero.py 
Cannot divide by zero
Message: integer division or modulo by zero
Class: <type 'exceptions.ZeroDivisionError'>

The hierarchy of exceptions

The exceptions are organized in a hierarchy, being Exception the parent of all exceptions.
#!/usr/bin/python

# interrupt.py

try:
while True:
pass
except KeyboardInterrupt:
print "Program interrupted"
The script starts and endless cycle. If we press Ctrl + C, we interrupt the cycle. Here, we caught the KeyboardInterrupt exception.
Exception
BaseException
KeyboardInterrupt
This is the hierarchy of the KeyboardInterrupt exception.
#!/usr/bin/python

# interrupt.py

try:
while True:
pass
except BaseException:
print "Program interrupted"
This example works too. The BaseException also catches the keyboard interruption. Among other exceptions.

User defined exceptions

We can create our own exceptions, if we want. We do it by defining a new exception class.
#!/usr/bin/python

# b.py


class BFoundError(Exception):
def __init__(self, value):
print "BFoundError: b character found at position %d" % value

string = "You make me want to be a better man."


pos = 0
for i in string:
if i == 'b':
raise BFoundError, pos
pos = pos + 1
In our code example, we have created a new exception. The exception is derived from the base Exception class. Say we hate the b letter. And if we find any occurence of that letter in a string, we raise our exception.
$ ./b.py 
BFoundError: b character found at position 20
Traceback (most recent call last):
File "./b.py", line 16, in <module>
raise BFoundError, pos
__main__.BFoundError

The cleanup

There is a finally keyword, which is always executed. No matter if the exception is raised or not. It is often used to do some cleanup of resources in a program.
#!/usr/bin/python

# cleanup.py

f = None

try:
f = file('indaclub', 'r')
contents = f.readlines()
for i in contents:
print i,
except IOError:
print 'Error opening file'
finally:
if f:
f.close()
In our example, we try to open a file. If we cannot open the file, an IOError is raised. In case we opened the file, we want to close the file handler. For this, we use the finally keyword. In the finally block we check if the file is opened or not. If it is opened, we close it. This is a common programming construct when we work with databases. There we similarly cleanup the opened database connections.
In this chapter, we have covered exceptions in Python.
Continue Reading

Python Packages

In this part of the Python programming tutorial, we will talk about packages.
A package is a collection of modules which have a common purpose. Technically a package is a directory which must have one special file called __init__.py.
There are several ways to manage Python code:
  • functions
  • classes
  • modules
  • packages
When we deal with large projects containing hundreds or thousands of modules, using packages is crucial. For example, we could put all database related modules in a database package, user interface code in ui package etc.
  
read.py
constants/
__init__.py
names.py
In our current working directory we have a constants directory and a read.py script.
The constants directory has two files. __init__.py file makes constants a Python package. The names.py is an ordinary module.
from names import names

print "initializing constants package"
The __init__.py file is initialized when the package is imported. Here we can control, what object will be available to the module, which imports the package.
#!/usr/bin/python

# names.py

names = ('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
The names.py module has a tuple of 5 names.
#!/usr/bin/python

# read.py

import constants

print constants.names
Finally, the read.py script imports the constants package. We print the names tuple from the package.
$ ./read.py 
initializing constants package
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')

We can also create subpackages. To access subpackages, we use the dot operator.
read.py
constants/
__init__.py
names.py
numbers/
__init__.py
integers.py
This is the new hierarchy. We have a subpackage called numbers.
from integers import integers
The __init__.py file in the numbers package has this one line.
#!/usr/bin/python

# integers.py


integers = (2, 3, 45, 6, 7, 8, 9)
The integers module defines a tuple of 7 integers. This tuple will be accessed from the read.py script.
#!/usr/bin/python

# read.py

import constants
import constants.numbers as int

print constants.names
print int.integers
The read.py script is modified a bit.
$ ./read.py 
initializing constants package
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
(2, 3, 45, 6, 7, 8, 9)

from package import * construct in a __init__.py file could cause problems on some older operating systems. Therefore a special variable __all__ has been introduced. This variable controls what objects will be imported from a package.
__all__ = ["names"]

print "initializing constants package"
Here we modify the __init__.py file in the constants directory.
#!/usr/bin/python

# read.py

from constants import names
import constants.numbers as int

print names.names
print int.integers
We also change the read.py script accordingly.
$ ./read.py
initializing constants package
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
(2, 3, 45, 6, 7, 8, 9)
The output is the same.
In this chapter, we have covered packages in Python.
Continue Reading

Python Modules Tutorials

In this part of the Python programming tutorial, we will talk about Python modules.
A module is a file in which we have Python code. The modules in Python have the .py extension.
There are several ways to manage Python code:
  • functions
  • classes
  • modules
  • packages
Python modules are used to organize Python code. For example, database related code is placed inside a database module, security code in a security module etc. Smaller Python scripts can have one module. But larger programs are split into several modules. Modules are grouped together to form packages.

Module names

A module name is the file name with the .py extension. When we have a file called empty.py, empty is the module name. The __name__ is a variable that holds the name of the module being referenced. The current module, the module being executed (called also the main module) has a special name: '__main__'. With this name it can be referenced from the Python code.
We have two files. empty.py and modulename.py. The second module is the main module, which is executed. It imports the first module. Modules are imported using the importkeyword.
$ cat empty.py
"""
An empty module
"""
This is empty.py module.
#!/usr/bin/python

import empty
import sys

print __name__
print empty.__name__
print sys.__name__
In this code example we import two modules. One built-in module (sys) and one custom module (empty). We print the names of modules to the console.
$ ./modulename.py 
__main__
empty
sys
The name of the module, which is being executed is always '__main__'. Other modules are named after the file name. Modules can be imported into other modules using the import keyword.

Locating modules

When a module is imported the interpreter first searches for a built-in module with that name. If not found, it then searches in a list of directories given by the variable sys.path. The sys.path is a list of strings that specifies the search path for modules. It consists of the current working directory, directory names specified in the PYTHONPATH environment variable plus some additional installation dependent directories. If the module is not found, an ImportError is raised.
#!/usr/bin/python

import sys
import textwrap

sp = sorted(sys.path)
dnames = ', '.join(sp)

print textwrap.fill(dnames)
The script prints all directories from sys.path variable.
import textwrap
The textwrap module is used for easy formatting of paragraphs.
sp = sorted(sys.path)
We retrieve a list of directories from the sys.path variable and sort them.
dnames = ', '.join(sp)
We make a string out of the list.
$ ./syspath.py 
/home/janbodnar/programming/python/modules,
/usr/lib/pymodules/python2.7, /usr/lib/python2.7, /usr/lib/python2.7
/dist-packages, /usr/lib/python2.7/dist-packages/PIL,
...
Sample output.

The import keyword

The import keyword can be used in several ways.
from module import *
This construct will import all Python definitions into the namespace of another module. There is one exception. Objects beginning with underscore character _ are not imported. They are expected to be used only internally by the module being imported. This way of importing modules is not recommended.
#!/usr/bin/python

from math import *

print cos(3)
print pi
This import construct has imported all definitions from the built-in math module. We can call the math funtcions directly, without referencing the math module.
$ ./everything.py 
-0.9899924966
3.14159265359
The use of this import construct may result in namespace pollution. We may have several objects of the same name and their definitions can be overriden.
#!/usr/bin/python

from math import *

pi = 3.14

print cos(3)
print pi
The example will print 3.14 to the console. Which may not be, what we wanted. The namespace pollution may become critical in larger projects.
The following example will show definitions, that are not being imported using this import construct.
#!/usr/bin/python

"""
names is a test module
"""

_version = 1.0

names = ["Paul", "Frank", "Jessica", "Thomas", "Katherine"]

def show_names():
for i in names:
print i

def _show_version():
print _version
This is the names.py module.
#!/usr/bin/python

from names import *

print locals()

show_names()
The _version variable and the _show_version() function are not imported into the private.py module. We don't see them in the namespace. The locals() function give us all the definitions available in the private module.
$ ./private.py 
{'__builtins__': <module '__builtin__' (built-in)>, '__file__': './private.py',
'show_names': <function show_names at 0xb7dd233c>,
'names': ['Paul', 'Frank', 'Jessica', 'Thomas', 'Katherine'],
'__name__': '__main__', '__doc__': None}
Paul
Frank
Jessica
Thomas
Katherine

from module import fun, var
This import construct imports only specific objects from a module. This way we import only definitions that we need.
#!/usr/bin/python

from math import sin, pi

print sin(3)
print pi
We import two objects from the math module. There is no way, how we could reference other definitions like e.g. a cos function.
#!/usr/bin/python

from names import _version, _show_version

print _version
_show_version()
We could also import definitions beginning with an underscore. But this is a bad practice.
$ ./imnames.py 
1.0
1.0

import module
The last construct is most widely used. It prevents the namespace pollution and enables to access all definitios from a module.
#!/usr/bin/python

import math

pi = 3.14

print math.cos(3)
print math.pi
print math.sin(3)
print pi
In this case, we reference the definitions via the module name. As we can see, we are able to use both pi variables. Our definition and the one from the math module.
$ ./widely.py 
-0.9899924966
3.14159265359
0.14112000806
3.14
#!/usr/bin/python

# importas.py

import math as m

print m.pi
print m.cos(3)
We can change the name through which we can reference the module. To do this, we use the as keyword.
$ ./importas.py 
3.14159265359
-0.9899924966

An ImportError is raised, if a module cannot be imported.
#!/usr/bin/python

try:
import empty2
except ImportError, e:
print 'Failed to import:', e
We have not created an empty2 module. Therefore an exception is raised.
$ ./importerror.py
Failed to import: No module named empty2
Output.

Executing modules

Modules can be imported into other modules or they can be also executed. Module authors often create a testing suite to test the module. Only if the module is executed as a script, the __name__ attribute equals to __main__.
We will demonstrate this on a fibonacci module. Fibonacci numbers is a sequence of numbers, where each is the sum of its two immediate predecessors.
#!/usr/bin/python

"""
A module containing the fibonacci
function.
"""

def fib(n):
a, b = 0, 1
while b < n:
print b,
(a, b) = (b, a + b)


# testing

if __name__ == '__main__':
fib(500)
The module can be normally imported as usual. The module can be also executed.
$ ./fibonacci.py 
1 1 2 3 5 8 13 21 34 55 89 144 233 377
If we do import the fibonacci module, the test is not executed automatically.
>>> import fibonacci as fib
>>> fib.fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
The fibonacci module is imported and the fib() function is executed.

The dir() function

The built-in dir() function gives a sorted list of strings containing the names defined by a module.
#!/usr/bin/python

"""
This is dirfun module
"""

import math, sys

version = 1.0

names = ["Paul", "Frank", "Jessica", "Thomas", "Katherine"]

def show_names():

for i in names:
print i

print dir(sys.modules['__main__'])
In this module, we import two system modules. We define a variable, a list and a function.
print dir(sys.modules['__main__']) 
The dir() function returns all the names available in the current namespace of the module. '__main__' is the name of the current module. The sys.modules is a dictionary that maps module names to modules which have already been loaded.
$ ./dirfun.py
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'math',
'names', 'show_names', 'sys', 'version']
We can see some built-in names like '__file__' or '__name__' and all the others, that we have defined.

The globals() function

The globals() function returns a dictionary that represents the current global namespace. It is a dictionary of global names and their values. It is the dictionary of the current module.
#!/usr/bin/python

import sys
import textwrap

version = 1.0

def myfun():
pass

gl = globals()
gnames = ', '.join(gl)

print textwrap.fill(gnames)
We use the globals() function to print all the global names of the current module.
$ ./globalsfun.py
__builtins__, __file__, textwrap, __package__, sys, myfun, version,
__name__, gl, __doc__
These are the global names of the current module.

The __module__ attribute

The __module__ class attribute has the name of the module in which the class is defined.
"""
module animals
"""

class Cat:
pass


class Dog:
pass
This are the contents of the animals.py file. We have two classes.
#!/usr/bin/python

from animals import Cat

class Being:
pass


b = Being()
print b.__module__

c = Cat()
print c.__module__
In this code we use the __module__ attribute.
from animals import Cat
From the animals module, we import the Cat class.
class Being:
pass
In the current module, we define a class Being.
b = Being()
print b.__module__
An instance of the Being class is created. We print the name of its module.
c = Cat()
print c.__module__
We create an object from the Cat class. We also print the module, where it was defined.
$ ./mclass.py
__main__
animals
The current module's name is __main__. And the Cat's module name is animals.
This chapter was about modules in Python.
Continue Reading

Python Object-oriented programming

In this part of the Python programming tutorial, we will talk about object oriented programming in Python.
There are three widely used programming paradigms there. Procedural programming, functional programming and object-oriented programming. Python supports both procedural and object-oriented programming. There is some limited support for f unctional programming too.
Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. (Wikipedia)
There are some basic programming concepts in OOP:
  • Abstraction
  • Polymorphism
  • Encapsulation
  • Inheritance
The abstraction is simplifying complex reality by modelling classes appropriate to the problem. The polymorphism is the process of using an operator or function in different ways for different data input. The encapsulation hides the implementation details of a class from other objects. The inheritance is a way to form new classes using classes that have already been defined.

Objects

Everything in Python is an object. Objects are basic building blocks of a Python OOP program.
#!/usr/bin/python

# objects.py

import sys


def function(): pass


print type(1)
print type("")
print type([])
print type({})
print type(())
print type(object)
print type(function)
print type(sys)
In this example we show, that all these entities are in fact objects. The type() function returns the type of the object specified.
$ ./objects.py 
<type 'int'>
<type 'str'>
<type 'list'>
<type 'dict'>
<type 'tuple'>
<type 'type'>
<type 'function'>
<type 'module'>

The class keyword

The previous objects were all built-in objects of the Python programming language. The user defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. From classes we construct instances. An instance is a specific object created from a particular class. For example, Huck might be an instance of a Dog class.
#!/usr/bin/python

# first.py

class First:
pass

fr = First()

print type(fr)
print type(First)
This is our first class. The body of the class is left empty for now. It is a convention to give classes a name that starts with a capital letter.
fr = First()
Here we create a new instance of the First class. Or in other words, we instantiate the First class. The fr is a reference to our new object.
$ ./first.py 
<type 'instance'>
<type 'classobj'>
Here we see that fr is an instance object and First is a class object.
Inside a class, we can define attributes and methods. An attribute is a characteristic of an object. This can be for example a salary of an employee. A method defines operations that we can perform with our objects. A method might define a cancellation of an account. Technically, attributes are variables and methods are functions defined inside a class.

Attributes

Attributes are characteristics of an object. A special method called __init__() is used to initialize the attributes of an object.
#!/usr/bin/python

# initialize.py


class Cat:
def __init__(self, name):
self.name = name


missy = Cat('Missy')
lucky = Cat('Lucky')

print missy.name
print lucky.name
In this code example, we have a Cat class. The special method __init__() is called automatically right after the object has been created.
 def __init__(self, name):
Each method in a class definition begins with a reference to the instance object. It is by convention named self. There is nothing special about the self name. We could name it this, for example. The name is the argument. The value is passed during the class instantiation.
 self.name = name
Here we pass an attribute to an instance object.
 missy = Cat('Missy')
lucky = Cat('Lucky')
Here we instantiate two objects. Missy and Lucky cats. The number of arguments must correspond to the __init__() method of the class definition. The 'Missy' and 'Lucky' strings become the name parameter of the __init__() method.
 print missy.name
print lucky.name
Here we print the instance variables of two cat objects. Each instance of a class can have their own attributes.
$ ./initialize.py 
Missy
Lucky
The attributes can be assigned dynamically, not just during initialization. This shows the next example.
#!/usr/bin/python

# dynamic.py

class Dynamic:
pass


d = Dynamic()
d.name = "Dynamic"
print d.name
We define and create an empty Dynamic class.
 d.name = "Dynamic"
This line of code creates a new name attribute.
$ ./dynamic.py 
Dynamic
So far, we have been talking about the instance attributes. In Python there are also so called class object attributes. Class object attributes are same for all instances of a class.
#!/usr/bin/python

# cat.py


class Cat:
species = 'mammal'

def __init__(self, name, age):
self.name = name
self.age = age


missy = Cat('Missy', 3)
lucky = Cat('Lucky', 5)

print missy.name, missy.age
print lucky.name, lucky.age

print Cat.species
print missy.__class__.species
print lucky.__class__.species
In our example, we have two cats with specific name and age attributes. Both cats share some characteristics. Missy and Lucky are both mammals. This is reflected in a class level attribute species. The attribute is defined outside any method name in the body of a class.
 print Cat.species
print missy.__class__.species
There are two ways, how we can access the class object attributes. Either via the name of the Cat class, or with the help of a special __class__ attribute.
$ ./cat.py 
Missy 3
Lucky 5
mammal
mammal
mammal

Methods

Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects. Methods are essential in encapsulation concept of the OOP paradigm. For example, we might have a connect() method in our AccessDatabase class. We need not to be informed, how exactly the method connect connects to the database. We only know, that it is used to connect to a database. This is essential in dividing responsibilities in programming. Especially in large applications.
#!/usr/bin/python

# circle.py


class Circle:
pi = 3.141592

def __init__(self, radius=1):
self.radius = radius

def area(self):
return self.radius * self.radius * Circle.pi

def setRadius(self, radius):
self.radius = radius

def getRadius(self):
return self.radius


c = Circle()

c.setRadius(5)
print c.getRadius()
print c.area()
In the code example, we have a Circle class. We define three new methods.
 def area(self):
return self.radius * self.radius * Circle.pi
The area() method returns the area of a circle.
 def setRadius(self, radius):
self.radius = radius
The setRadius() method sets a new value for a radius attribute.
 def getRadius(self):
return self.radius
The getRadius() method returns the current radius.
 c.setRadius(5)
The method is called on an instance object. The c object is paired with the self parameter of the class definition. The number 5 is paired with the radius parameter.
$ ./circle.py 
5
78.5398
In Python, we can call methods in two ways. There are bounded and unbounded method calls.
#!/usr/bin/python

# methods.py

class Methods:
def __init__(self):
self.name = 'Methods'

def getName(self):
return self.name


m = Methods()

print m.getName()
print Methods.getName(m)
In this example, we demostrate both method calls.
 print m.getName()
This is the bounded method call. The Python interpreter automatically pairs the m instance with the self parameter.
 print Methods.getName(m)
And this is the unbounded method call. The instance object is explicitly given to the getName() method.
$ ./methods.py 
Methods
Methods

Inheritance

The inheritance is a way to form new classes using classes that have already been defined. The newly formed classes are called derivedclasses, the classes that we derive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of base classes (ancestors).
#!/usr/bin/python

# inherit.py


class Animal:
def __init__(self):
print "Animal created"

def whoAmI(self):
print "Animal"

def eat(self):
print "Eating"


class Dog(Animal):
def __init__(self):
Animal.__init__(self)
print "Dog created"

def whoAmI(self):
print "Dog"

def bark(self):
print "Woof!"


d = Dog()
d.whoAmI()
d.eat()
d.bark()
In this example, we have two classes. Animal and Dog. The animal is the base class, the Dog is the derived class. The derived class inherits the functionality of the base class. It is shown by the eat() method. The derived class modifies existing behaviour of the base class, shown by the whoAmI() method. Finally, the derived class extends the functionality of the base class, by defining a new bark() method.
 class Dog(Animal):
def __init__(self):
Animal.__init__(self)
print "Dog created"
We put the ancestor classes in round brackets after the name of the descendant class. If the derived class provides it's own __init__() method, it must explicitly call the base class __init__() method.
$ ./inherit.py 
Animal created
Dog created
Dog
Eating
Woof!

Polymorphism

The polymorphism is the process of using an operator or function in different ways for different data input. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently. (wikipedia)
#!/usr/bin/python

# basic.py


a = "alfa"
b = (1, 2, 3, 4)
c = ['o', 'm', 'e', 'g', 'a']

print a[2]
print b[1]
print c[3]
Python programming language uses polymorphism extensively in built-in types. Here we use the same indexing operator for three different data types.
$ ./basic.py 
f
2
g
Polymorphism is most commonly used when dealing with inheritance.
#!/usr/bin/python

# polymorphism.py


class Animal:
def __init__(self, name=''):
self.name = name

def talk(self):
pass


class Cat(Animal):
def talk(self):
print "Meow!"


class Dog(Animal):
def talk(self):
print "Woof!"


a = Animal()
a.talk()

c = Cat("Missy")
c.talk()

d = Dog("Rocky")
d.talk()
Here we have two species. A dog and a cat. Both are animals. The Dog class and the Cat class inherit the Animal class. They have a talk() method, which gives different output for them.
$ ./polymorphism.py 
Meow!
Woof!

Special Methods

Classes in Python programming language can implement certain operations with special method names. These methods are not called directly, but by a specific language syntax. This is similar to what is known as operator overloading in C++ or Ruby.
#!/usr/bin/python

# book.py

class Book:
def __init__(self, title, author, pages):
print "A book is created"
self.title = title
self.author = author
self.pages = pages

def __str__(self):
return "Title:%s , author:%s, pages:%s " % \
(self.title, self.author, self.pages)

def __len__(self):
return self.pages

def __del__(self):
print "A book is destroyed"


book = Book("Inside Steve's Brain", "Leander Kahney", 304)

print book
print len(book)
del book
In our code example, we have a book class. Here we introduce four special methods. The __init__(), __str__(), __len__() and the __del__() methods.
book = Book("Inside Steve's Brain", "Leander Kahney", 304)
Here we call the __init__() method. The method creates a new instance of a Book class.
print book
The print keyword calls the __str__() method. This method should return an informal string representation of an object.
print len(book)
The len() function invokes the __len__() method. In our case, we print the number of pages of our book.
del book
The del keyword deletes an object. It calls the __del__() method.
In the next example we implement a vector class and demonstrate addition and substraction operations on it.
#!/usr/bin/python

# vector.py


class Vector:

def __init__(self, data):
self.data = data

def __str__(self):
return repr(self.data)

def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return Vector(data)

def __sub__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] - other.data[j])
return Vector(data)


x = Vector([1, 2, 3])
y = Vector([3, 0, 2])
print x + y
print y - x

  def __add__(self, other):
data = []
for j in range(len(self.data)):
data.append(self.data[j] + other.data[j])
return Vector(data)
Here we implement the addition operation of vectors. The __add__() method is called, when we add two Vector objects with the + operator. Here we add each member of the respective vectors.
$ ./vector.py 
[4, 2, 5]
[2, -2, -1]
In this part of the Python tutorial, we have covered object-oriented programming in Python.
Continue Reading

Python Files Tutorial

In this part of the Python programming tutorial, we will talk about files.
Everything in Python is an object. Everything in UNIX is a file.

Standard I/O

There are three basic I/O connections. Standard input, standard output and standard error. Standard input is the data that goes to the program. The standard input comes from a keyboard. Standard output is where we print our data with the print keyword. Unless redirected, it is the terminal console. The standard error is a stream where programs write their error messages. It is usually the text terminal.
Conforming to the UNIX philosophy, the standard I/O streams are file objects.

Standard input

Standard input is the data that goes to the program.
#!/usr/bin/python

# name.py

import sys


print 'Enter your name: ',

name = ''

while True:
c = sys.stdin.read(1)
if c == '\n':
break
name = name + c

print 'Your name is:', name
In order to work with standard I/O streams, we must import the sys module. The read() method reads one character from the standard input. In our example we get a promt saying "Enter your name". We enter our name and press enter. The enter key generates the new line character: \n.
$ ./name.py 
Enter your name: Jan
Your name is: Jan
For getting input we can use higher level functions: input() and raw_input() .
The input() function prints a prompt if it is given, reads input and evaluates it.
#!/usr/bin/python

# input.py


data = input('Enter expression: ')

print 'You have entered:', data
$ ./input.py 
Enter expression: 3*3
You have entered: 9
The raw_input() prints a prompt if it is present. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
#!/usr/bin/python

# rawinput.py


name = raw_input('Enter your name: ')

print 'You have entered:', name

Standard output

The standard output is where we print our data.
#!/usr/bin/python

# stdout.py

import sys

sys.stdout.write('Honore de Balzac, Father Goriot\n')
sys.stdout.write('Honore de Balzac, Lost Illusions\n')
In the example, we write some text to the standard output. This is in our case the terminal console. We use the write() method.
$ ./stdout.py 
Honore de Balzac, Father Goriot
Honore de Balzac, Lost Illusions
The print keyword puts some text into the sys.stdout by default.
#!/usr/bin/python

# printkeyw.py

print 'Honore de Balzac, The Splendors and Miseries of Courtesans'
print 'Honore de Balzac, Gobseck'
We do not provide the sys.stdout explicitly. It is done behind the scenes. There is another form of the print keyword. If we want, we can use it to write some data into a regular file.
#!/usr/bin/python

# works.py

f = open('works', 'w')

print >> f, 'Beatrix'
print >> f, 'Honorine'
print >> f, 'The firm of Nucingen'

f.close()
We open a file and write three titles of Balzac books into it. This form is also called print chevron.
$ cat works
Beatrix
Honorine
The firm of Nucingen

Redirection

File objects can be redirected. In the following example, we redirect the standard output to a regular file.
#!/usr/bin/python

# redirect.py

import sys


f = open('output', 'w')
sys.stdout = f

print 'Lucien'
sys.stdout.write('Rastignac\n')
sys.stdout.writelines(['Camusot\n', 'Collin\n'])

sys.stdout = sys.__stdout__

print 'Bianchon'
sys.stdout.write('Lambert\n')
In the redirect.py script, we redirect a standard output to a regular file called output. Then we restore the original standard output back. The original value of the std.output is kept in a special sys.__stdout__variable.
$ ./redirect.py 
Bianchon
Lambert
$ cat output
Lucien
Rastignac
Camusot
Collin

The open function

The open() function is used to open files in our system.
open(filename, [mode='r'], [bufsize])
The filename is the name of the file to be opened. The mode indicates, how the file is going to be opened. For reading, writing, appending etc. The bufsize is an optional buffer. It specifies the file's desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of that size. A negative bufsize means to use the system default. If omitted, the system default is used.
The file modes:
ModeMeaning
rReading
wWriting
aAppending
bBinary data
+Updating
The following little script prints the contents of a file.
#!/usr/bin/python

# wantme.py

f = open('ifyouwantme', 'r')

for line in f:
print line,

f.close()
We open a file in read mode. We traverse the contents of the file with the for loop. In the end, we close the file object.
$ ./wantme.py 
Are you really here or am I dreaming
I can't tell dreams from truth
for it's been so long since I have seen you
I can hardly remember your face anymore

When I get really lonely
and the distance causes our silence
I think of you smiling
with pride in your eyes a lover that sighs

...
In the next example, we do the same. Now we use the readline() method.
#!/usr/bin/python

# wantme2.py

f = open('ifyouwantme', 'r')

while True:
line = f.readline()
if not line: break
else: print line,

f.close()
There is yet another simple way, how we can print the contents of a file. Using the readlines()method. The readlines() method reads all the contents of a file into the memory. This is not applicable for very large files, though.
#!/usr/bin/python

# wantme3.py

f = open('ifyouwantme', 'r')

contents = f.readlines()

for i in contents:
print i,

f.close()
In the next example, we will demonstrate writing to a file. We will write a simple strophe into a regular file.
#!/usr/bin/python

# strophe.py

text = """Incompatible, it don't matter though
'cos someone's bound to hear my cry
Speak out if you do
You're not easy to find\n"""


f = open('strophe', 'w')
f.write(text)

f.close()
This time we open the file in a w mode, so that we can write into it. If the file does not exist, it is created. If it exists, it is overwritten.
$ cat strophe
Incompatible, it don't matter though
'cos someone's bound to hear my cry
Speak out if you do
You're not easy to find

The pickle module

So far, we have been working with simple textual data. What if we are working with objects rather than simple text? For such situations, we can use the pickle module. This module serializes Python objects. The Python objects are converted into byte streams and written to text files. This process is called pickling. The inverse operation, reading from a file and reconstructing objects is called deserializing or unpickling.
#!/usr/bin/python

# pickle.py

import pickle

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def getName(self):
return self.name

def getAge(self):
return self.age


person = Person('Monica', 15)
print person.getName()
print person.getAge()

f = open('monica', 'w')
pickle.dump(person, f)
f.close()

f = open('monica', 'r')
monica = pickle.load(f)
f.close()

print monica.getName()
print monica.getAge()
In our script, we define a Person class. We create one person. We pickle the object using the dump()method. We close the file, open it again for reading. Unpickle the object using the load() method.
$ ./monica.py 
Monica
15
Monica
15
$ file monica
monica: ASCII text
The file to which we write is a simple text file.
In this part of the Python tutorial, we covered files.
Continue Reading

Functions in Python

In this part of the Python programming tutorial, we will talk about functions.
A function is a piece of code in a program. The function performs a specific task. The advantages of using functions are:
  • Reducing duplication of code
  • Decomposing complex problems into simpler pieces
  • Improving clarity of the code
  • Reuse of code
  • Information hiding
Functions in Python are first-class citizens. It means that functions have equal status with other objects in Python. Functions can be assigned to variables, stored in collections or passed as arguments. This brings additional flexibility to the language.
There are two basic types of functions. Built-in functions and user defined ones. The built-in functions are part of the Python language. Examples are: dir(), len() or abs(). The user defined functions are functions created with the def keyword.

Defining functions

A function is created with the def keyword. The statements in the block of the function must be indented.
def function():
pass
The def keyword is followed by the function name with round brackets and a colon. The indented statements form a body of the function.

The function is later executed when needed. We say that we call the function. If we call a function, the statements inside the function body are executed. They are not executed until the function is called.
myfunc()
To call a function, we specify the function name with the round brackets.
#!/usr/bin/python

"""
The ret.py script shows how to work with
functions in Python.
author: Jan Bodnar
ZetCode, 2011
"""


def showModuleName():
print __doc__

def getModuleFile():
return __file__

a = showModuleName()
b = getModuleFile()

print a, b
The string at the top of the script is called the documentation string. It documents the current script. The file in which we put Python code is called a module. We define two functions. The first function will print the module doc string. The second will return the path of our module. Function may or may not return a value. If they explicitly do not return a value, they implicitly return None. The __doc__ and __file__ are special state attributes. Note, that there are two underscores on both sides of the attribute.
$ ./ret.py 

The ret.py script shows how to work with
functions in Python.
author: Jan Bodnar
ZetCode, 2011

None ./ret.py

Definitions of functions must precede their usage. Otherwise the interpreter will complain with a NameError.
#!/usr/bin/python

def f1():
print "f1()"

f1()
#f2()

def f2():
print "f2()"
In the above example we have two definitions of functions. One line is commented. Function call cannot be ahead of its definition.
#f2()

def f2():
print "f2()"
We can call the f2() only after its definition. Uncommenting the line we get a NameError.

Where to define functions

Functions can be defined inside a module, a class or another function. Function defined inside a class is called a method.
#!/usr/bin/python

class Some:

@staticmethod
def f():
print "f() method"

def f():
print "f() function"

def g():
def f():
print "f() inner function"
f()

Some.f()
f()
g()
In this example, we define an f() function in all possible places.
class Some:

@staticmethod
def f():
print "f() method"
A static method is defined with a decorator in a Some class.
def f():
print "f() function"
The function is defined in a module.
def g():
def f():
print "f() inner function"
f()
Here the f() function is defined inside another g() function. It is an inner function.
Some.f()
f()
g()
The static method is called by specifying the class name, the dot operator and the function name with square brackets. Other functions are called using their names and square brackets.
$ ./defining.py
f() method
f() function
f() inner function
Output.

Functions are objects

Functions in Python are objects. They can be manipulated like other objects in Python. Therefore functions are called first-class citizens. This is not true in other OOP languages like Java or C#.
#!/usr/bin/python

def f():
"""This function prints a message """
print "Today it is a cloudy day"

print isinstance(f, object)
print id(f)

print f.func_doc
print f.func_name
In this script we show, that our function is an object too.
def f():
"""This function prints a message """
print "Today it is a cloudy day"
We define an f() function. It prints a message to the console. It has a documentation string.
print isinstance(f, object)
The isinstance() function checks, if the f() function is an instance of the object. All objects in Python inherit from this base entity.
print id(f)
Each object in Python has a unique id. The id() function returns the object's id.
print f.func_doc
print f.func_name
Objects may have attributes. Here we print two attributes of the function.
$ ./fobj.py
True
3077407212
This function prints a message
f
Output.

Objects can be stored in collections and passed to functions.
#!/usr/bin/python

def f():
pass

def g():
pass

def h(f):
print id(f)

a = (f, g, h)

for i in a:
print i

h(f)
h(g)
We define three functions. We place them in a tuple and pass them to a function.
a = (f, g, h)

for i in a:
print i
We place three function objects in a tuple and traverse it with a for loop.
h(f)
h(g)
We pass the f(), g() functions to the h() function.
$ ./fobj2.py
<function f at 0xb7664fb4>
<function g at 0xb766c1b4>
<function h at 0xb766c3ac>
3076935604
3076964788
Output of the fobj.py script.

Three kinds of functions

Looking from a particular point of view, we can discern three kinds of functions. Functions that are always available for usage, functions that are contained within external modules, which must be imported and functions defined by a programmer with the def keyword.
#!/usr/bin/python

from math import sqrt

def cube(x):
return x * x * x

print abs(-1)
print cube(9)
print sqrt(81)
Three kinds of functions are present in the above code.
from math import sqrt
The sqrt() function is imported from the math module.
def cube(x):
return x * x * x
The cube() function is a custom defined function.
print abs(-1)
The abs() function is a built-in function readily accessible. It is part of the core of the language.

The return keyword

A function is created to do a specific task. Often there is a result from such a task. The return keyword is used to return values from a function. A function may or may not return a value. If a function does not have a return keyword, it will send a None value.
#!/usr/bin/python

def showMessage(msg):
print msg

def cube(x):
return x * x * x

x = cube(3)
print x

showMessage("Computation finished.")
print showMessage("Ready.")
We have two functions defined. One uses the returnkeyword, one does not.
def showMessage(msg):
print msg
The showMessage() function does not return explicitly a value. It shows a message on the console.
def cube(x):
return x * x * x
The cube() functions computes an expression and returns its result with the return keyword.
x = cube(3) 
In this line we call the cube() function. The result of the computation of the cube() function is returned and assigned to the x variable. It holds the result value now.
showMessage("Computation finished.")
We call the showMessage() function with a message as a parameter. The message is printed to the console. We do not expect a value from this function.
print showMessage("Ready.")
This code produces two lines. One is a message printed by the showMessage() function. The other is the None value, which is implicitly sent by functions without the return statement.
$ ./return.py
27
Computation finished.
Ready.
None
Example output.

We can send more that one value from a function. The objects after the returnkeyword are separated by commas.
#!/usr/bin/python

n = [1, 2, 3, 4, 5]

def stats(x):
mx = max(x)
mn = min(x)
ln = len(x)
sm = sum(x)

return mx, mn, ln, sm

mx, mn, ln, sm = stats(n)
print stats(n)

print mx, mn, ln, sm
There is a definition of a stats() function. This function returns four values.
return mx, mn, ln, sm
The return keyword sends back four numbers. The numbers are separated by a comma character. In fact, we have sent a tuple containing these four values. We could also return a list instead of a tuple.
mx, mn, ln, sm = stats(n)
The returned values are assigned to local variables.
$ ./return2.py 
(5, 1, 5, 15)
5 1 5 15
Output.

Function redefinition

Python is dynamic in nature. It is possible to redefine an already defined function.
#!/usr/bin/python

from time import gmtime, strftime


def showMessage(msg):
print msg

showMessage("Ready.")

def showMessage(msg):
print strftime("%H:%M:%S", gmtime()),
print msg

showMessage("Processing.")
We define a showMessage() function. Later we provide a new definition of the same function.
from time import gmtime, strftime
From the time module we import two functions which are used to compute the current time.
def showMessage(msg):
print msg
This is the first definition of a function. It only prints a message to the console.
def showMessage(msg):
print strftime("%H:%M:%S", gmtime()),
print msg
Later in the source code, we set up a new definition of the showMessage() function. The message is preceded with a timestamp.
$ ./redefinition.py
Ready.
23:49:33 Processing.
Ouput of the script.

Function arguments

Most functions accept arguments. Arguments are values, that are sent to the function. The functions process the values and optionally return some value back.
#!/usr/bin/python

def C2F(c):
return c * 9/5 + 32

print C2F(100)
print C2F(0)
print C2F(30)
In our example, we convert Celsius temperature to Fahrenheit. The C2F function accepts one argument c, which is the Celsius temperature.
$ ./fahrenheit.py 
212
32
86
The arguments in Python functions may have implicit values. An implicit value is used, if no value is provided.
#!/usr/bin/python

def power(x, y=2):
r = 1
for i in range(y):
r = r * x
return r

print power(3)
print power(3, 3)
print power(5, 5)
Here we created a power function. The function has one argument with an implicit value. We can call the function with one or two arguments.
$ ./power.py 
9
27
3125
Python functions can specify their arguments with a keyword. This means, that when calling a function, we specify both a keyword and a value. When we have multiple arguments and they are used without keywords, the order in which we pass those arguments is crucial. If we expect a name, age, sex in a function without keywords, we cannot change their order. If we use keywords, we can.
#!/usr/bin/python

def display(name, age, sex):
print "Name: ", name
print "Age: ", age
print "Sex: ", sex


display("Lary", 43, "M")
display("Joan", 24, "F")
In this example, the order in which we specify the arguments is important. Otherwise, we get incorrect results.
$ ./persons.py 
Name: Lary
Age: 43
Sex: M
Name: Joan
Age: 24
Sex: F
#!/usr/bin/python

# person2.py

def display(name, age, sex):
print "Name: ", name
print "Age: ", age
print "Sex: ", sex


display(age=43, name="Lary", sex="M")
display(name="Joan", age=24, sex="F")
Now we call the functions with their keywords. The order may be changed, although it is not recommended to do so. Note, that we cannot use a non-keyword argument after a keyword argument. This would end in a syntax error.
display("Joan", sex="F", age=24)
This is a legal construct. A non-keyword argument may be followed by keyword arguments.
display(age=24, name="Joan", "F")
This will end in a syntax error. A non-keyword argument may not follow keyword arguments.
Functions in Python can even accept arbitrary number of arguments.
#!/usr/bin/python

def sum(*args):
'''Function returns the sum
of all values'''
r = 0
for i in args:
r += i
return r


print sum.__doc__
print sum(1, 2, 3)
print sum(1, 2, 3, 4, 5)
We use the * operator to indicate, that the function will accept arbitrary number of arguments. The sum() function will return the sum of all arguments. The first string in the function body is called the function documentation string. It is used to document the function. The string must be in triple quotes.
$ ./summation.py 
Function returns the sum
of all values
6
15
We can also use the ** construct in our functions. In such a case, the function will accept a dictionary. The dictionary has arbitrary length. We can then normally parse the dictionary, as usual.
#!/usr/bin/python

def display(**details):

for i in details:
print "%s: %s" % (i, details[i])


display(name="Lary", age=43, sex="M")
This example demonstrates such a case. We can provide arbitrary number of key-value arguments. The function will handle them all.
$ ./person.py 
age: 43
name: Lary
sex: M

Passing by reference

Parameters to functions are passed by reference. Some languages pass copies of the objects to functions. Passing objects by reference has two important conclusions. The process is faster than if copies of objects were passed. Mutable objects that are modified in functions are permanently changed.
#!/usr/bin/python

n = [1, 2, 3, 4, 5]

print "Original list:", n

def f(x):
x.pop()
x.pop()
x.insert(0, 0)
print "Inside f():", x

f(n)

print "After function call:", n
In our example, we pass a list of integers to a function. The object is modified inside the body of the function. After calling the function, the original object, the list of integers is modified.
def f(x):
x.pop()
x.pop()
x.insert(0, 0)
print "Inside f():", x
In the body of the function we work with the original object. Not with a copy of the object. In many programming languages we woud receive a copy of an object by default.
$ ./byreference.py
Original list: [1, 2, 3, 4, 5]
Inside f(): [0, 1, 2, 3]
After function call: [0, 1, 2, 3]
Once the list was modified it was modified for good.

Global and local variables

Next we will talk about how variables are used in Python functions.
#!/usr/bin/python

name = "Jack"

def f():
name = "Robert"
print "Within function", name

print "Outside function", name
f()
A variable defined in a function body has a local scope. It is valid only within the body of the function.
$ ./local.py 
Outside function Jack
Within function Robert
Output.
#!/usr/bin/python

name = "Jack"

def f():
print "Within function", name

print "Outside function", name
f()
By default, we can get the contents of a global variable inside the body of a function. But if we want to change a global variable in a function, we must use the globalkeyword.
$ ./global.py 
Outside function Jack
Within function Jack
#!/usr/bin/python

name = "Jack"

def f():
global name
name = "Robert"
print "Within function", name


print "Outside function", name
f()
print "Outside function", name
Now, we will change the contents of a global name variable inside a function.
global name 
name = "Robert"
Using the global keyword, we reference the variable defined outside the body of the function. The variable is given a new value.
$ ./global2.py 
Outside function Jack
Within function Robert
Outside function Robert

Anonymous functions

It is possible to create anonymous functions in Python. Anonymous functions do not have a name. With the lambda keyword, little anonymous functions can be created. Anonymous functions are also called lambda functions by Python programmers. They are part of the functional paradigm incorporated in Python.
Lambda functions are restricted to a single expression. They can be used wherever normal functions can be used.
#!/usr/bin/python

y = 6

z = lambda x: x * y
print z(8)
This is a small example of the lambda function.
z = lambda x: x * y
The lambda keyword creates an anonymous function. The x is a parameter, that is passed to the lambda function. The parameter is followed by a colon character. The code next to the colon is the expression that is executed, when the lambda function is called. The lambda function is assigned to the z variable.
print z(8)
The lambda function is executed. The number 8 is passed to the anonymous function and it returns 48 as the result. Note that z is not a name for this function. It is only a variable to which the anonymous function was assigned.
$ ./lambda.py
48
Output of the example.

The lambda function can be used elegantly with other functional parts of the Python language, like map() or filter()functions.
#!/usr/bin/python

cs = [-10, 0, 15, 30, 40]

ft = map(lambda t: (9.0/5)*t + 32, cs)
print ft
In the example we have a list of celsius temperatures. We create a new list containing temperatures in fahrenheit.
ft = map(lambda t: (9.0/5)*t + 32, cs)
The map() function applies the anonymous function to each element of the cs list. It creates a new ft list containing the computed fahrenheit temperatures.
$ ./lambda2.py
[14.0, 32.0, 59.0, 86.0, 104.0]
Example output.
This chapter was about functions in Python.
Continue Reading

Keywords in Python

In this part of the Python programming tutorial, we will introduce all keywords in Python language.

List of keywords

The following is a list of keywords for the Python programming language.
and       del       from      not       while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
Python is a dynamic language. It changes during time. The list of keywords may change in the future.
#!/usr/bin/python

# keywords.py

import sys
import keyword


print "Python version: ", sys.version_info
print "Python keywords: ", keyword.kwlist
This script prints the version of Python and it's actual keyword list.

print keyword

The print keyword is use to print numbers and characters to the console.
#!/usr/bin/python

# tutorial.py

print "*" * 24
print "*" * 24
print "*" * 24
print
print "\tZetCode"
print
print "*" * 24
print "*" * 24
print "*" * 24
The print keyword without any text will line feed.
$ ./tutorial.py 
************************
************************
************************

ZetCode

************************
************************
************************

Control flow

The while keyword is a basic keyword for controlling the flow of the program. The statements inside the while loop are executed, until the expression evaluates to False.
#!/usr/bin/python

# sum.py

numbers = [22, 34, 12, 32, 4]
sum = 0

i = len(numbers)

while (i != 0):
i -= 1
sum = sum + numbers[i]

print "The sum is: ", sum
In our script we want to calculate the sum of all values in the numbers list. We utilize the while loop. We determine the length of the list. The while loop is executed over and over again, until the i is equal to zero. In the body of the loop, we decrement the counter and calculate the sum of values.
The break keyword is used to interrupt the cycle, if needed.
#!/usr/bin/python

# testbreak.py

import random

while (True):
val = random.randint(1, 30)
print val,
if (val == 22):
break
In our example, we print random integer numbers. If the number equals to 22, the cycle is interrupted with the break keyword.
$ ./testbreak.py 
14 14 30 16 16 20 23 15 17 22

The next example shows the continue keyword. It is used to interrupt the current cycle, without jumping out of the whole cycle. New cycle will begin.
#!/usr/bin/python

# testcontinue.py

import random

num = 0

while (num < 1000):
num = num + 1
if (num % 2) == 0:
continue
print num,
In the example we print all numbers smaller than 1000, that cannot be divided by number 2.

The if keyword is the commonest used control flow keyword. The if keyword is used to determine, which statements are going to be executed.
#!/usr/bin/python

# licence.py

age = 17

if age > 18:
print "Driving licence issued"
else:
print "Driving licence not permitted"
The if keyword tests if the the person is older than 18. If the condition is met, the driving licence is issued. Otherwise, it is not. The else keyword is optional. The statement after the else keyword is executed, unless the condition is True.
Next we will see, how we can combine the statements using the elif keyword. Stands for else if.
#!/usr/bin/python

# hello.py

name = "Luke"

if name == "Jack":
print "Hello Jack!"
elif name == "John":
print "Hello John!"
elif name == "Luke":
print "Hello Luke!"
else:
print "Hello there!"
If the first test evaluates to False, we continue with the next one. If none of the tests is True, the else statement is executed.
$ ./hello.py 
Hello Luke!
Output.

The for keyword is used to iterate over items of a collection in order that they appear in the container.
#!/usr/bin/python

# lyrics.py

lyrics = """\
Are you really here or am I dreaming
I can't tell dreams from truth
for it's been so long since I have seen you
I can hardly remember your face anymore
"""


for i in lyrics:
print i,
In the example, we have a lyrics variable having a strophe of a song. We iterate over the text and print the text character by character. The comma in the print statement prevents from printing each character on a new line.
$ ./lyrics.py 
A r e y o u r e a l l y h e r e o r a m I d r e a m i n g
I c a n ' t t e l l d r e a m s f r o m t r u t h
f o r i t ' s b e e n s o l o n g s i n c e I h a v e s e e n y o u
I c a n h a r d l y r e m e m b e r y o u r f a c e a n y m o r e
This is the output of the script.

Boolean expressions

First we will introduce keywords, that work with boolean values and expressions. is, or, and and not.
#!/usr/bin/python

# objects.py

print None == None
print None is None

print True is True

print [] == []
print [] is []

print "Python" is "Python"
The == operator tests for equality The is keyword tests for object identity. Whether we are talking about the same object. Note, that multiple variables may refer to the same object.
$ ./objects.py 
True
True
True
True
False
True
The output might be surprising for you. In Python language, there is only one None and one True object. That's why True is equal and also identical to True. There is only one truth out there, anyway. The empty list [] is equal to another empty list []. But they are not identical. Python has put them into two different memory locations. They are two distinct objects. Hence the is keyword returns False. On the other hand, "Python" is "Python" returns True. This is because of optimalisation. If two string literals are equal, they have been put to same memory location. A string is an immutable entity. No harm can be done.

The not keyword negates a boolean value.
#!/usr/bin/python

# grades.py

grades = ["A", "B", "C", "D", "E", "F"]

grade = "L"

if grade not in grades:
print "unknown grade"
In our examle we test, whether the grade value is from the list of possible grades.
$ ./grades.py 
unknown grade

The keyword and is used, if all conditions in a boolean expression must be met.

#!/usr/bin/python

# youngmale.py

sex = "M"
age = 26

if age < 55 and sex == "M":
print "a young male"
In our example, we test, if two conditions are met. The "young male" is printed to the console, if variable age is less than 55 and variable sex is equal to M.
 $ ./youngmale.py 
a young male

The keyword or is used if at least one condition must be met.
#!/usr/bin/python

# name.py

name = "Jack"

if ( name == "Robert" or name == "Frank" or name == "Jack"
or name == "George" or name == "Luke"):
print "This is a male"
If at least one of the expressions is true, the print statement is executed.
When we work with and/or keywords in Python programming language, short circuit evaluation takes place. Short circuit evaluation means that the second argument is only evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and evaluates to false, the overall value must be false; and when the first argument of or evaluates to true, the overall value must be true. (wikipedia)
A typical example follows.
#!/usr/bin/python

x = 10
y = 0

if (y != 0 and x/y < 100):
print "a small value"
The first part of the expression evaluates to False. The second part of the expression is not evaluated. Otherwise, we would get a ZeroDivisionError.

Modules

The following keywords are used with modules. Modules are files, in which we organize our Python code.
The import keyword is used to import other modules into a Python script.
#!/usr/bin/python

# pi.py

import math

print math.pi
We use the import keyword to import the math module into the namespace of our script. We print the PI value.

We use the as keyword, if we want to give a module a different alias.
#!/usr/bin/python

# rand.py

import random as rnd

for i in range(10):
print rnd.randint(1, 10),
In this case, we import the random module. We will print ten random integer numbers. We give the random module a different alias, namely rnd. In the script we reference the module with the new alias. Notice, that we cannot name the script random.py or rnd.py. We would get errors.
$ ./rand.py 
1 2 5 10 10 8 2 9 7 2

The from keyword is used for importing a specific variable, class or a function from a module.
#!/usr/bin/python

# testfrom.py

from sys import version

print version
From the sys module, we import the version variable. If we want to print it, we do not need to use the module name. The version variable was imported directly to our namespace and we can reference it directly.
$ ./testfrom.py 
2.5.1 (r251:54863, Mar 7 2008, 03:41:45)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)]

Functions

Here we will describe keywords associated with functions. The def keyword is used to create a new user defined function. Functions are objects in which we organize our code.
#!/usr/bin/python

# function.py


def root(x):
return x * x

a = root(2)
b = root(15)

print a, b
The example demonstrates a simple new function. The function will calculate the square of a number. The return key is closely connected with a function definition. The keyword exits the function and returns a value. The value is than assigned to the a, b variables.

The lambda keyword creates a new anonymous function. An anonymous function is a function, which is not bound to a specific name. It is also called an inline function.
!/usr/bin/python

# lambda.py

for i in (1, 2, 3, 4, 5):
a = lambda x: x * x
print a(i),
As you can see in the previous example, we do not create a new function with a def keyword. Instead of that we use an inline function on the fly.
$ ./lambda.py 
1 4 9 16 25

If we want to access variables defined outside functions, we use the global keyword.
#!/usr/bin/python

# testglobal.py

x = 15

def function():
global x
x = 45

function()
print x
Normally, assigning to x variable inside a function, we create a new local variable, which is valid only in that function. But if we use the global keyword, we change a variable ouside the function definition.
$ ./testglobal.py 
45

Exceptions

Next we will work with keywords, that are used with exception handling.
$ cat films
Fargo
Aguirre, der Zorn Gottes
Capote
Grizzly man
Notes on a scandal
This is a file, containing some film titles. In the code example, we are going to read it.
#!/usr/bin/python

# files.py

f = None

try:
f = open('films', 'r')
for i in f:
print i,
except IOError:
print "Error reading file"

finally:
if f:
f.close()
We try to read a films file. If no exception occurs, we print the contents of the file to the console. There might be an exception. For example, if we provided an incorrect file name. In such a case a IOError exception is raised. The except keyword catches the exception and executes its code. The finally keyword is always executed in the end. We use it to clean up our resources.

In the next example, we show how to create a user defined exception using the raise keyword.
#!/usr/bin/python

# userexception.py

class YesNoException(Exception):
def __init__(self):
print 'Invalid value'


answer = 'y'

if (answer != 'yes' and answer != 'no'):
raise YesNoException
else:
print 'Correct value'
In the example, we expect only yes/no values. For other possibilities, we raise an exception.
$ ./userexception.py 
Invalid value
Traceback (most recent call last):
File "./userexception.py", line 13, in <module>
raise YesNoException
__main__.YesNoException

Other keywords

The del keyword deletes objects.
#!/usr/bin/python

# delete.py

a = [1, 2, 3, 4]

print a
del a[:2]
print a
In our example, we have a list of four integer numbers. We delete the first numbers from the list. The outcome is printed to the console.
$ ./delete.py 
[1, 2, 3, 4]
[3, 4]
Output.

The pass keyword does nothing. It is a very handy keyword in some situations.
 def function():
pass
We have a function. This function is not implemented yet. It will be later. The body of the function must not be empty. So we can use a pass keyword here, instead of printing something like "function not implemented yet" or similar.

The assert keyword is used for debugging purposes. We can use it for testing conditions, that are obvious to us. For example, we have a program that calculates salaries. We know that the salary cannot be less than zero. So we might put such an assertion to the code. If the assertion fails, the interpreter will complain.
#!/usr/bin/python

# salary.py

salary = 3500
salary -= 3560 # a mistake was done

assert salary > 0
During the execution of the program a mistake was done. The salary becomes a negative number.
$ ./salary.py 
Traceback (most recent call last):
File "./salary.py", line 9, in <module>
assert salary > 0
AssertionError
The execution of the script will fail with an AssertionError.

The class keyword is the most important keyword in object oriented programming. It is used to create new user defined objects.
#!/usr/bin/python

# square.py

class Square:
def __init__(self, x):
self.a = x

def area(self):
print self.a * self.a


sq = Square(12)
sq.area()
In the code example, we create a new Square class. Then we instantiate the class and create an object. We compute the area of the square object.

The exec keyword executes Python code dynamically.
#!/usr/bin/python

# execution.py

exec("for i in [1, 2, 3, 4, 5]: print i,")
We print five numbers from a list using a for loop. All within the exec keyword.
$ ./execution.py 
1 2 3 4 5

Finally, we mention the in keyword.
#!/usr/bin/python

# inkeyword.py

print 4 in (2, 3, 5, 6)

for i in range(25):
print i,
In this example, the in keyword tests if the number four is in the tuple. The second usage is traversing a tuple in a for loop. The built-in function range() returns integers 0 .. 24.
$ ./inkeyword.py 
False
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
The yield keyword is used with generators.
#!/usr/bin/python

# yieldkeyword.py


def gen():
x = 11
yield x

it = gen()

print it.next()
The yield keyword exits the generator and returns a value.
$ ./yieldkeyword.py 
11
In this part of the Python tutorial, we have covered Python keywords.
Continue Reading