2. An Introduction

The Python Programming language has been around for quite a while. Development work was started on the first version by Guido Van Rossum in 1989. Since then, it has grown to become a highly loved and revered language with a broad array of applications.

The Python interpreter and the extensive standard library that come with the interpreter are available in source or binary form for all major platforms from the Python Web site free of charge. The site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.

The Python interpreter can be extended with new functions and data types implemented in C, C++ or any other language that is callable from C. Python is also suitable as an extension language for customizable applications. One of the most notable features of Python is the easy and white-space aware syntax.

This book is intended as a concise intermediate level treatise on the Python programming language. The content of this book is for those that have been through a beginner level introduction to Python or have some experience in a different object-oriented programming language such as Java and want to gain a more in-depth understanding of the Python programming language. It is not intended as an introductory tutorial for beginners, although programmers with some experience in other languages may find the concise tutorial included instructive.

This book covers only a handful of topics but tries to provide in-depth coverage of these topics. It starts with a short tutorial introduction to get the reader up to speed with the basics of Python; experienced programmers from other object-oriented languages such as Java may find that this is all the introduction to Python that they need. Next, is a discussion of the Python object model; then it moves on to discussing object-oriented programming in Python. Functions and functional programming follow, and then a discussion of meta-programming techniques and their applications comes next. The remaining chapters cover generators, a complex but fascinating topic in Python, modules and packaging, and Python runtime services. In between, we use intermezzos to discuss topics that are worth knowing because of the added understanding they provide.

2.1 The Evolution of Python

Guido Van Rossum started work on a language that he christened Python in December 1989. Guido had been part of the team that worked on the ABC programming language as part of the Amoeba operating systems in the 1980s at CWI (Centrum Wiskunde & Informatica) in Amsterdam. Although he liked the ABC programming language, he was frustrated by several features or lack of thereof. Guido wanted a high-level programming language that would speed up the development of utilities for the Amoeba project and ABC was not the answer. The ABC programming language would, however, play a very influential role in the development of Python as Guido took parts he liked from the language and provided solutions to aspects of the ABC programming language that he found frustrating.

Guido released the first version of the Python programming language in February 1991. This release was object-oriented, had a module system, included exception handling, functions, and the core data types of list, dict, str and others. Version 1.0 was released in January 1994, and included functional programming constructs such as lambda, map, filter and reduce. Python 1.2 was the last version released while Guido was still at CWI. In 1995, Van Rossum continued his work on Python at the Corporation for National Research Initiatives (CNRI) in Reston, Virginia where he released several versions of the language with indirect funding support from DARPA.

By version 1.4, Python had acquired several new features including the Modula-3 inspired keyword arguments and built-in support for complex numbers. It also included a basic form of data hiding by name mangling. Python 1.5 was released on December 31, 1997, while Python 1.6 followed on September 5, 2000.

Version 2.0 was released on October 16, 2000, and it introduced list comprehensions, a feature borrowed from the functional programming languages SETL and Haskell as well as a garbage collection system capable of collecting reference cycles. Python 2.2 was the first major update to the Python type system. This update saw the unification of Python’s in-built types and user-defined classes written in Python into one hierarchy. This single unification made Python’s object model purely and consistently object-oriented. This update to the class system of Python added several features that improved the programming experience. These included:

  1. The ability to subclass inbuilt types such as dicts and lists.
  2. The addition of static and class methods
  3. The addition of properties defined by get and set methods.
  4. An update to meta-classes, __new__() and super() methods, and MRO algorithms.

The next major milestone was Python 3.0, released on December 2, 2008. Python 3 rectifies certain fundamental design flaws that could not be fixed without breaking full backwards compatibility with the 2.x series.

2.2 Python 2 vs Python 3

The most visible and disruptive change to the Python ecosystem has been the introduction of Python 3. The significant changes introduced into Python 3 include the following:

  1. print() is now a function
  2. Some well known python APIs such as range(), dict.keys(), dict.values() return views and iterators rather than lists improving efficiency when they are used.
  3. The rules for ordering comparisons have been simplified. For example, a heterogeneous list cannot be sorted, because all the elements of a list must be comparable to each other.
  4. The integer types have been whittled down to only one, i.e. int. long is also an int.
  5. The division of two integers returns a float instead of an integer. A script should use the// operator To get the integer dividend.
  6. All texts are now Unicode; the encoded Unicode text is represented as binary data, and any attempt to mix text and data will result in an exception. This change breaks backwards compatibility with Python 2.x versions.
  7. Python 3 also saw the introduction of some new syntax such as function annotations, the nonlocal statement, extended iterable unpacking, set literals, dictionary comprehensions etc.
  8. Python 3 also saw an update to some syntax such as that of exception handling, meta-class specification, list comprehensions etc.

The full details of changes from Python 2 to Python 3 are on the Python website. The rest of the book will assume the use of Python 3.4.

2.3 The Python Programming Language

The Python programming language refers to the language as documented in the language reference. There is no official language specification, but the language reference provides enough details to guide anyone implementing the Python programming language. The implementation of the Python programming language available on the Python website is an implementation written in C and commonly referred to as CPython, the reference implementation. However, there are other Python implementations in different languages. Popular among these are PyPy: Python implemented in Python and Jython: Python implemented in Java. The rest of this book uses the reference CPython version that is available at the Python website.