The Wayback Machine - https://web.archive.org/web/20150716235818/http://docforge.com/wiki/Python
Software Development Resources
Create account | Log in | Log in with OpenID | Help

Python

From DocForge

Python is a multiparadigm language, supporting object oriented programming, functional programming, imperative programming, and scripting in a natural way. The wide variety of available modules for it make many kinds of programming easier.

Existing implementations include an interpreter (REPL) and automatic byte code compilation, as well as an extensive and flexible standard library. The syntax is clean and simple, offering literals for common container types and treating everything (including literals) as an object. Because of all these things, Python lends itself well to rapid application development and use as a glue language. Most implementations are multiplatform.

Byte code compilation saves a bytecode copy of an application after it runs for the first time, and uses that copy to load the application faster the second and subsequent times it is run. As with Java, byte code compilation makes it easier to implement a 'write once, run anywhere' language.

Contents

[edit] Features

Notable features include

[edit] Zen of Python

Python is built with a focus on a simple set of core principles. These are outlined in the Zen of Python by Tim Peters. The list can be read by launching any Python interpreter and typing import this.

[edit] Reference

[edit] History

Python was conceived by Guido van Rossum as a scripting language for the Amoeba operating system and as a followup to the teaching language ABC. Perhaps subconsciously dubious of the eventual outcome of Amoeba, van Rossum designed Python to be easily ported to multiple platforms.

The name the language is a reference to the Monty Python comedy group, not the snake (though the O'Reilly book does use the snake on the cover). References to Monty Python skits abound in the language documentation and code samples; also the name of the official package index, the Python Cheese Shop (no spoilers here).

Python now rivals Perl as the standard scripting language on Linux-based operating systems (though Python is now much more than a scripting language), and the runtime is included by default on most Linux distributions. A major milestone in programming language mind-share: Python was selected as the development language of choice for the One Laptop Per Child project. Google is also a proponent of the language, using it for much of their server-side code and for gluing together existing high-performance code. It's also one of their three "approved" languages, along with C++ and Java.

Changes to the language specification are hashed out in public via mailing lists and more officially through Python Enhancement Proposals (PEPs). The current stable version of Python is 2.5, and the next release, 2.6, is slated for April 2008. A major revision to the language, known for years as Python 3000, will be released in later 2008 and will break backwards compatibility.

[edit]Implementations

  • CPython -- Guido van Rossum's reference implementation, written in C. As Python is not standardised by ISO, ECMA et al, this implementation is the de-facto standard and the place where new core language features are introduced first.
  • IronPython -- Python adapted to the .NET virtual machine. This gives Python access to the .NET platform's libraries, and allows Python to compile to CLR bytecode, allowing the resulting program to run on any platform that supports the .NET runtime. Performance is slightly better than CPython in most cases.[1]
  • Jython -- Python on the Java Virtual Machine. As with IronPython, this allows Python code byte-compiled with Jython to run anywhere the JVM is available. In recent years the Jython implementation has fallen noticeably behind the CPython reference implementation, currently implementing Python 2.3 compared to the reference version 2.5.
  • PyPy -- Python implemented entirely in a restricted subset of Python itself, called RPython. While bootstrapping is more difficult, the goal is to allow straightforward creation of Python compilers for any platform -- Java, .NET, Parrot, LLVM, or even machine code -- using the PyPy base. Currently, PyPy runs about half as fast as CPython, though PyPy's speed has been increasing by a full order of magnitude every year or two. The PyPy project recently ended a 28-month period of funding by the EU, which resulted in much research, development, and a JIT compiler.
  • Stackless Python -- An alternative C implementation that overcomes certain vestigial roadblocks in the standard Python implementation, allowing interesting new capabilities for continuations and other exotic but useful language features. This reimplementation was rejected by the core CPython team, allegedly because the corresponding reimplementation in Jython would be intractable.
  • Unladen swallow - An optimization branch of CPython, intended to be fully compatible and significantly faster.

[edit]Efficiency

As an interpreted programming language, Python executes with an efficiency comparable to similar languages such as Perl and Ruby. This is a tradeoff favoring ease of use and portability over performance. For example, Python uses dynamic typing (specifically, duck typing), evaluating the suitability of an object for an operation at runtime. In contrast, languages with static typing (such as Java, C++, and OCaml) make this determination at compilation time, allowing the program to execute more efficiently when it's finally run.

To improve the efficiency of a Python program, it's possible to write processing-intensive portions of the code in C or C++, and call this code from Python as needed. This code, written properly, can run 5-10 times faster than the pure Python equivalent -- unfortunately, it makes the code more complex and slightly less portable. (Common side effects of programming in C and C++.) The Python language documentation described the API for extending Python with C/C++ code or embedding Python in a C/C++ program.

Fortunately, several tools exist to help Python and C/C++ communicate:

  • SWIG
  • Boost::Python
  • ctypes -- included in Python 2.5's standard library, and also available separately. Defines a very simple FFI.
  • Pyrex and Cython lets you write code that mixes Python and C data types any way you want, and compiles it into a C extension for Python.

As with any language, the key to writing efficient code is knowing how the compiler/interpreter works best. In Python, function calls tend to be relatively expensive, while built-in constructs like list comprehensions translate more efficiently into bytecode and will evaluate faster (since the work's being done in C, by the implementation, rather than in Python, by you).

Contrived Example: Python currently supplies the built-in function map(), which applies a given function to each item in a given list. Very functional. But the same thing can be done faster with list comprehensions.

Say we want to get string representations of every number from 1 to a million. This is one way to create that list:

my_numbers = map(str, range(1, 1000001)) # str() is built in

This is a faster way, using list comprehensions:

my_numbers = [str(x) for x in range(1, 1000001)] # More intuitive, too, no?

And there are other, more efficient ways to do it, too. This redundancy conflicts with the Zen of Python, and it's why the functions map(), filter() and reduce() will be removed from the standard module in Python 3.0, despite the outrage of Lisp programmers everywhere.

[edit] Uses

Python has a wide variety of common uses.

[edit]Web Frameworks

A multitude of web application frameworks have been created for Python (an arguably unfortunate situation in comparison to that of Ruby programmers, who have a much easier choice to make). Here's a rundown:

  • Django - Popular, robust framework useful for a wide variety of web applications
  • Flask - A microframework based on Werkzeug and Jinja 2
  • pyjamas - Similar to the Google Web Toolkit (which is for Java)
  • Pylons - Aims to be more like Rails.
  • Pyroxide - Full MVC framework atop mod_python with a truly object oriented ORM (Object-relational mapping) layer built in.
  • Turbogears - Similar to Django.
  • Twisted - More of a lower-level networking framework, really.
  • Web.py - Simple and Pythonic
  • Zope - Very powerful and baroque framework designed for large content management systems. Older.
  • Web2py - A recent MVC framework very similar to Django but easier to set up. The template system is python embedded in HTML, so it compiles to bytecode and it's allegedly faster than other python template systems. It has nothing to do with Web.py.

[edit]External Links & References

References:

  1. IronPython Home Page

Discuss

  • Disqus
close