## The Python language
``Python``:inxx
### About Python
Python is a general-purpose high-level programming language.
Its design philosophy emphasizes programmer productivity and code readability. It has a minimalist core syntax with very few basic commands and simple semantics, but it also has a large and comprehensive standard library, including an Application Programming Interface (API) ``API``:inxx to many of the underlying operating system (OS) functions. Python code, while minimalist, defines built-in objects such as linked lists (``list``), tuples (``tuple``), hash tables (``dict``), and arbitrarily long integers (``long``).
Python supports multiple programming paradigms, including object-oriented (``class``), imperative (``def``), and functional (``lambda``) programming. Python has a dynamic type system and automatic memory management using reference counting (similar to Perl, Ruby, and Scheme).
Python was first released by Guido van Rossum in 1991. The language has an open, community-based development model managed by the non-profit Python Software Foundation. There are many interpreters and compilers that implement the Python language, including one in Java (Jython) but, in this brief review, we refer to the reference C implementation created by Guido.
You can find many tutorials, the official documentation and library references of the language on the official Python website.``python``:cite
For additional Python references, we can recommend the books in ref.``guido``:cite and ref.``lutz``:cite .
You may skip this chapter if you are already familiar with the Python language.
### Starting up
``shell``:inxx
The binary distributions of web2py for Microsoft Windows or Apple OS X come packaged with the Python interpreter built into the distribution file itself.
BaseException
| +-- ReferenceError
| +-- RuntimeError
| | +-- NotImplementedError
| +-- SyntaxError
| | +-- IndentationError
| | +-- TabError
| +-- SystemError
| +-- TypeError
| +-- ValueError
| | +-- UnicodeError
| | +-- UnicodeDecodeError
| | +-- UnicodeEncodeError
| | +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+ +-- ImportWarning
+ +-- UnicodeWarning
``:code
For a detailed description of each of them, refer to the official Python documentation.
web2py exposes only one new exception, called ``HTTP``. When raised, it causes the program to return an HTTP error page (for more on this refer to Chapter 4).
Any object can be raised as an exception, but it is good practice to raise objects that extend one of the built-in exception classes.
### ``def...return``
``def``:inxx ``return``:inxx
Functions are declared using ``def``. Here is a typical Python function:
``
>>> def f(a, b):
return a + b
>>> print f(4, 2)
6
``:code
There is no need (or way) to specify types of the arguments or the return type(s). In this example, a function ``f`` is defined that can take two arguments.
This is a very powerful module. It provides functions that can serialize almost
>>> class MyClass(object): pass
>>> myinstance = MyClass()
>>> myinstance.x = 'something'
>>> a = [1 ,2, {'hello':'world'}, [3, 4, [myinstance]]]
``:code
and now:
``
>>> import cPickle
>>> b = cPickle.dumps(a)
>>> c = cPickle.loads(b)
``:code
In this example, ``b`` is a string representation of ``a``, and ``c`` is a copy of ``a`` generated by de-serializing ``b``.
cPickle can also serialize to and de-serialize from a file:
``
>>> cPickle.dump(a, open('myfile.pickle', 'wb'))
>>> c = cPickle.load(open('myfile.pickle', 'rb'))
``:code
-
## The Python language
``Python``:inxx
### About Python
Python is a general-purpose high-level programming language.
Its design philosophy emphasizes programmer productivity and code readability. It has a minimalist core syntax with very few basic commands and simple semantics, but it also has a large and comprehensive standard library, including an Application Programming Interface (API) ``API``:inxx to many of the underlying operating system (OS) functions. Python code, while minimalist, defines built-in objects such as linked lists (``list``), tuples (``tuple``), hash tables (``dict``), and arbitrarily long integers (``long``).
Python supports multiple programming paradigms, including object-oriented (``class``), imperative (``def``), and functional (``lambda``) programming. Python has a dynamic type system and automatic memory management using reference counting (similar to Perl, Ruby, and Scheme).
Python was first released by Guido van Rossum in 1991. The language has an open, community-based development model managed by the non-profit Python Software Foundation. There are many interpreters and compilers that implement the Python language, including one in Java (Jython) but, in this brief review, we refer to the reference C implementation created by Guido.
You can find many tutorials, the official documentation and library references of the language on the official Python website.``python``:cite
For additional Python references, we can recommend the books in ref.``guido``:cite and ref.``lutz``:cite .
You may skip this chapter if you are already familiar with the Python language.
### Starting up
``shell``:inxx
The binary distributions of web2py for Microsoft Windows or Apple OS X come packaged with the Python interpreter built into the distribution file itself.
BaseException
| +-- ReferenceError
| +-- RuntimeError
| | +-- NotImplementedError
| +-- SyntaxError
| | +-- IndentationError
| | +-- TabError
| +-- SystemError
| +-- TypeError
| +-- ValueError
| | +-- UnicodeError
| | +-- UnicodeDecodeError
| | +-- UnicodeEncodeError
| | +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
- +-- ImportWarning
- +-- UnicodeWarning
``:code
For a detailed description of each of them, refer to the official Python documentation.
web2py exposes only one new exception, called ``HTTP``. When raised, it causes the program to return an HTTP error page (for more on this refer to Chapter 4).
Any object can be raised as an exception, but it is good practice to raise objects that extend one of the built-in exception classes.
### ``def...return``
``def``:inxx ``return``:inxx
Functions are declared using ``def``. Here is a typical Python function:
``
>>> def f(a, b):
return a + b
>>> print f(4, 2)
6
``:code
There is no need (or way) to specify types of the arguments or the return type(s). In this example, a function ``f`` is defined that can take two arguments.
This is a very powerful module. It provides functions that can serialize almost
>>> class MyClass(object): pass
>>> myinstance = MyClass()
>>> myinstance.x = 'something'
>>> a = [1 ,2, {'hello':'world'}, [3, 4, [myinstance]]]
``:code
and now:
``
>>> import cPickle
>>> b = cPickle.dumps(a)
>>> c = cPickle.loads(b)
``:code
In this example, ``b`` is a string representation of ``a``, and ``c`` is a copy of ``a`` generated by de-serializing ``b``.
cPickle can also serialize to and de-serialize from a file:
``
>>> cPickle.dump(a, open('myfile.pickle', 'wb'))
>>> c = cPickle.load(open('myfile.pickle', 'rb'))
``:code
-
-
-
-

Similarly, we can obtain a list of methods of the object "1" with the command ``dir``:
``
>>> dir(1)
['__abs__', ..., '__xor__']
``:code
Similarly, we can obtain a list of methods of the object "1" with the command ``dir``:
``
>>> dir(1)
-['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
-'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
-'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__',
-'__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__',
-'__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__',
-'__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__',
-'__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__',
-'__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__',
-'__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__',
'__str__', '__sub__', '__truediv__', '__xor__']
``:code

Here is a more complex class:
``
>>> class MyClass(object):
>>> z = 2
>>> def __init__(self, a, b):
+>>> self.x = a
+>>> self.y = b
>>> def add(self):
>>> return self.x + self.y + self.z
>>> myinstance = MyClass(3, 4)
>>> print myinstance.add()
9
``:code
Here is a more complex class:
``
>>> class MyClass(object):
>>> z = 2
>>> def __init__(self, a, b):
->>> self.x = a, self.y = b
>>> def add(self):
>>> return self.x + self.y + self.z
>>> myinstance = MyClass(3, 4)
>>> print myinstance.add()
9
``:code