Python_(language) Python_(language)

Python (language) - Definition and Overview

Related Words: Afghan, Afghani, Afrikaans, Ainu, Akan, Akkadian, Albanian, Aleut, Algonquian, Algonquin, Amharic, Anatolian, Andaman, Apache, Arabic, Aramaic, Araucanian, Arawak, Arawakan, Armenian, Aryan, Assamese

Python is an interpreted, interactive programming language created by Guido van Rossum in 1990, originally as a scripting language for Amoeba OS capable of making system calls. Python is often compared to Tcl, Perl, Scheme, Java, and Ruby. Python is developed as an open source project, managed by the non-profit Python Software Foundation. Python 2.4 was released on November 30, 2004.

Contents

Philosophy

Python is a multi-paradigm language, like Perl, Oz or C++ and unlike Smalltalk or Haskell. This means that, rather than forcing coders to adopt one particular style of coding, it permits several. Object orientation, structured programming, functional programming, aspect-oriented programming, and more recently, design by contract are all supported. Python is dynamically type-checked and uses garbage collection for memory management. An important feature of Python is dynamic name resolution, which allows method and variable names to be bound during program execution.

Python has many similarities to Perl. However, Python's designers reject Perl's exuberant syntax in favor of a more spare, less cluttered one. As with Perl, Python's developers expressly promote a particular "culture" or ideology (http://python.org/dev/culture.html) based on what they want the language to be, favoring language forms they see as "beautiful", "explicit" and "simple". For the most part, Perl and Python users differ in their interpretation of these terms and how they are best implemented.

Another important goal of the Python developers is to make using Python fun. This is reflected in the origin of the name (after the television series Monty Python's Flying Circus); in the common practice of using Monty Python references in example code; and in an occasionally playful approach to tutorials and reference materials.

Although Python is sometimes classified as a "scripting language", it has been used to develop many large software projects such as the Zope application server and the Mnet and BitTorrent file sharing system. It is also extensively used by Google. Python proponents prefer to call it a high level dynamic programming language, on the grounds that "scripting language" implies a language that is only used for simple shell scripts or that refers to a language like JavaScript: much simpler and, for most purposes, less capable than "real" programming languages such as Python.

Another important goal of the language is ease of extensibility. New built-in modules are easily written in C or C++. Python can also be used as an extension language for existing modules and applications that need a programmable interface.

Though the designer of Python is somewhat hostile to functional programming and the Lisp tradition, there are significant parallels between the philosophy of Python and that of minimalist Lisp-family languages such as Scheme. Many past Lisp programmers have found Python appealing for this reason.

Data types and structures

Python has a broad range of basic data types. Alongside conventional integer and floating point arithmetic, it transparently supports arbitrarily large integers and complex numbers.

It supports the usual panoply of string operations, with one exception: strings in Python are immutable objects, so any string operation that might elsewhere alter a string (such as a substitution of characters) will instead return a new string.

Python values, not variables, carry type—meaning that Python is a dynamically typed language, like Lisp and unlike Java or C. All values are passed by reference, not by value.

Among dynamically typed languages, Python is moderately type-checked — it is more strictly typed than Perl, for example. Implicit conversion is defined for numeric types, so one may validly multiply a complex number by a long integer (for instance) without explicit casting. However, there is no implicit conversion between (e.g.) numbers and strings; unlike in Perl, a number is an invalid argument to a string operation.

Collection types

One of the fundamental aspects of Python is the concept of collection (or container) types. In general a collection is an object that contains other objects in a way that is easily referenced or indexed. Collections come in two basic forms: mappings and sequences.

Mappings are unordered types implemented in the form of dictionaries which "map" a set of objects, or keys, to elements in a set of values much like mathematical functions. (Python dictionaries are mutable; there is no built-in immutable mapping type, though one can easily be made if desired.) For example, one could a define a dictionary having a string "foo" mapped to the integer 42 or vice versa. This is done under the covers via a hash function which makes for faster lookup times, but is also the culprit for a dictionary's lack of order and has the effect of not allowing one to use a built-in mutable object (i.e. another dictionary) as a key. This is consistent with Python passing values by reference: you wouldn't want someone changing your key while you weren't looking. Dictionaries also reside at the core of all Python objects and classes as a way to store member attributes and methods in a name (string) to attribute (object) relationship (see Object system).

On the other side of the collections coin are the ordered sequential types called lists, tuples, and strings. All sequences are indexed positionally (0 through length − 1) and all but strings can contain any type of object (strings only allowing characters, which are represented in Python as one-character strings). Both strings and tuples are immutable, making them perfect candidates for dictionary keys. Lists, on the other hand, are mutable; you can insert, delete, modify, or append elements in place, but Python does not provide a built-in hash function for them.

Furthermore, a third collection type was recently added to the core language. It's an unindexed, unordered collection that contains no duplicate items. There are two varieties: "set" and "frozenset", the difference being that "set" is mutable and "frozenset" is immutable. Elements can be of any type given that it is hashable and immutable. Thus a "frozenset" can be an element of a "set" whereas the opposite is not true.

Python also provides extensive collection manipulating abilities such as built in containment checking and implied iteration, thus "for element in list" (see Functional programming).

Object system

The Python type system is well integrated with the class system. Although the built-in data types are not precisely classes, a class can inherit from a type. Thus it is possible to extend strings, dictionaries or even integers. Python also supports multiple inheritance.

The language supports extensive introspection of types and classes. Types can be read and compared—indeed, as in Smalltalk, types are instances of a type. The attributes of an object can be extracted as a dictionary.

Operators can be overloaded in Python by defining special member functions—for instance, defining __add__ on a class permits one to use the + operator on members of that class. (Compare C++'s operator+ and similar method names.)

Syntax

Python was designed to be highly readable. It has a simple visual layout, uses English keywords frequently where other languages use punctuation, and has notably fewer syntactic constructions than many structured languages such as C, Perl, or Pascal.

For instance, Python has only two structured loop forms—for, which loops over elements of a list or iterator (like Perl foreach); and while, which loops as long as a boolean expression is true. It thus lacks C-style complex for, a do...while, and Perl's until, though of course equivalents can be expressed. Likewise, it has only if...elif...else for branching—no switch or labeled goto (goto was implemented (http://www.entrian.com/goto/) as a joke for April 1st 2004, in an add-on module).

Syntactical significance of indentation

One unusual aspect of Python's syntax is the method used to delimit program blocks. Sometimes termed "the whitespace thing", it is one aspect of Python syntax that many programmers otherwise unfamiliar with Python have heard of, since it is nearly unique among currently widespread languages (another language sharing this feature is Haskell).

In so-called "free-format" languages that use the block structure ultimately derived from Algol—including Pascal, C, Perl, and many others—blocks of code are set off with braces ({ }) or keywords such as Pascal's begin and end. In all these languages, however, programmers conventionally indent the code within a block, to set it off visually from the surrounding code.

Python, instead, borrows a feature from the lesser-known language ABC—instead of punctuation or keywords, it uses this indentation itself to indicate the run of a block. A brief example will make this clear. Here are C and Python recursive functions which do the same thing—computing the factorial of an integer:

Factorial function in C:

int factorial(int x) {      
    if (x == 0) {                      
        return 1;                   
    } else {
        return x * factorial(x-1);
    }
}

Factorial function in Python:

def factorial(x):
    if x == 0:
        return 1
    else:
        return x * factorial(x-1)

Some programmers used to Algol-style languages, in which whitespace is semantically empty, at first find this confusing or even offensive. A few have drawn unflattering comparison to the column-oriented style used on punched-card Fortran systems. When Algol was new, it was a major development to have "free-form" languages in which only symbols mattered and not their position on the line. Emailing Python code, pasting it from one application into another, or any other operation in which the whitespace is not preserved can destroy a program's functionality.

To Python programmers, however, "the whitespace thing" is simply an extrapolation of a convention that programmers in Algol-style languages already follow anyway. They also point out that the free-form syntax has the disadvantage that, since indentation is ignored, good indentation cannot be enforced. Thus, incorrectly indented code may be misleading, since a human reader and a compiler could interpret it differently.

Comments and docstrings

Python has two ways to annotate Python code. One is by using comments to indicate what some part of the code does.

 def getline():
     return sys.stdin.readline()       # Get one line and return it

Comments begin with the hash character ("#") and are terminated by the end of line. Python does not support comments that span more than one line. The other way is to use docstrings (documentation string), that is strings delimited with either """ or '''.

 def getline():
     """Get one line from stdin and return it."""
     return sys.stdin.readline()

Docstrings can be as large as the programmer wants and contain line breaks. They must always begin in the first line of the body of a function, class or or other Python entity. They can also be written at the top of a module. In contrast with comments, docstrings are themselves Python objects and is part of the interpreted code that Python runs. That means that a running program can retrieve its own docstrings and do manipulations with that info. But the normal usage is to give other programmers information about how to invoke the object being documented in the docstring.

There are tools availible that can extract the docstrings to generate an API documentation from the code. Docstring documentation can also be accessed from the interpreter with the help() function, or from the shell with the pydoc command.

A hack is to use docstrings to comment out blocks of code. However, if the block that is being out-commented contains docstrings it wont work and the code wont run. A better approach is to use an editor that has a multi-line comment function, which most Python-aware editors have.

Functional programming

As mentioned above, another strength of Python is the availability of a functional programming style. As may be expected, these make working with lists and other collections much more straightforward. One such construction is the list comprehension, introduced from the functional language Haskell, as seen here in calculating the first five powers of two:

numbers = [1, 2, 3, 4, 5]
powers_of_two = [2**n for n in numbers]

The Quicksort algorithm can be expressed elegantly using list comprehensions:

def qsort(L):
  if L == []: return []
  return qsort([x for x in L[1:] if x< L[0] ]) + L[0:1] + \
         qsort([x for x in L[1:] if x>=L[0] ])

Although naive execution of this form of Quicksort is less space-efficient than forms which alter the sequence in-place, it is often cited as an example of the expressive power of list comprehensions.

Because Python permits functions as arguments, it is also possible to express more subtle functional constructs, such as the continuation.

Lambda

Python's lambda keyword may misdirect some functional-programming fans. Python lambda blocks may contain only expressions, not statements. Thus, they are not the most general way to return a function for use in higher-order functions. Instead, the usual practice is to define and return a function using a locally-scoped name, as in the following example of a simple curried function:

def add_and_print_maker(x):
    def temp(y):
        print "%d + %d = %d" % (x, y, x+y)
    return temp

The function can also be implemented with nested lambdas, as would be done in Scheme. To do this requires working around the Python lambda's limitation, by defining a function to encapsulate the print statement:

def print_func(obj):
   print obj
add_and_print_maker = \
   lambda(x): lambda(y): \
      print_func("%d + %d = %d" % (x, y, x+y))

The resulting add_and_print_maker functions perform identically: given a number x they return a function which, when given a number y, will print a sentence of arithmetic. Although the first style may be more common, the second can be more clear to programmers with a functional-programming background.

Python's unique style for the binary boolean operators and and or creates another unique functional feature. Using those two operators, any type of control flow can be implemented within lambda expressions [1] (http://www-106.ibm.com/developerworks/linux/library/l-prog.html). They are usually used for simpler purposes, however. See the heading logical operators below.

Generators

Introduced in Python 2.2 as optional feature and finalized in version 2.3, generators are Python's mechanism for lazy evaluation of a function that would otherwise return a space-prohibitive or computationally intensive list. The uses of generators are similar to the uses of Scheme streams.

One example from the python.org website:

def generate_ints(N):
   for i in xrange(N):
       yield i

You can now use the generator generate_ints:

for i in generate_ints(N):
   print i

Note that the variable N should be defined before executing the second piece of code.

The definition of a generator appears identical to that of a function, except the keyword yield is used in place of return. However, a generator is an object with persistent state, which can repeatedly enter and leave the same dynamic extent. A generator call can then be used in place of a list, or other structure whose elements will be iterated over. Whenever the for-loop in the example requires the next item, the generator is called, and yields the next item.

Logical operators

In Python 2.2 and earlier, the expressions "", 0, 0.0, 0e0, 0j, None, (), [], {}, etc. are false, and everything else is true. When using binary Boolean operators in Python, the syntax is to have the operator be in between the two statements in question. So to see if the statements x==5 and 3 are true, one would write "x==5 and 3". To evaluate this, the interpreter would first check if x==5 returned true. If it didn't, it would return 0, but since it did, it goes on to the next statement. Next, it checks if 3 is true. Since 3 is true, 3 is returned. If three weren't true, 0 would be returned. If the order of all of this were reversed to 3 and x==5, 1 would be returned because that's what x==5 evaluates to (because 1 is the default truth value). The or function works similarly. To find out if "2/3 or 5" is true, the interpreter first finds the truth value of 2/3. Since 2/3 evaluates to 0, as described above, it would return false. If it had returned true, then its value would be returned. Next, the interpreter looks at the second expression. Since, in this case, it returns true, 5 would be returned. It is common in Python to write expressions such as print p or q to take advantage of this feature.

Later in Python 2.2.1 the keywords* True and False were added and, as a result, all of the binary comparison operators (==, >, etc) return either True or False, while the rest of the aforementioned boolean operations (and, etc) still return the value that the last expression evaluated to. Thus the expression "2 == 2" will return the value True and "2 == 2 and 5" still returns the integer 5.

*Under the hood, in Python 2.3, True and False are builtin objects of type bool which is a great example of the object oriented nature of Python. In 2.2.1–2.2.3, they are names for the int objects 1 and 0, respectively.

Object-oriented programming

Python's support for object oriented programming paradigm is vast. It supports polymorphism, not only under the statically typed "polymorphism of classes inherited from the same base class", but fully in the Liskov Substitution Principle-sense for all objects. And everything in Python is an object, including classes, functions, numbers and modules. Python also has support for metaclasses, an advanced tool for enhancing classes' functionality. Naturally, inheritance, including multiple inheritance, is supported. It has limited support for private variables using name mangling. See the "Classes" section of the tutorial (http://www.python.org/doc/current/tut/node11.html#SECTION0011600000000000000000) for details. Many Python users don't feel the need for private variables, though. The slogan "We're all consenting adults here" is used to describe this attitude. Some consider information hiding to be unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals.

From the tutorial: As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to "break into the definition."

OOP doctrines such as the use of accessor methods to read data members are not enforced in Python. Just as Python offers functional-programming constructs but does not attempt to demand referential transparency (in contrast with Haskell), it offers (and extensively uses!) its object system but does not demand OOP behavior (in contrast with Java or Smalltalk). Moreover, it is always possible to redefine the class using properties so that when a certain variable is set or retrieved in calling code, it really invokes a function call, so that foo.x = y might really invoke foo.set_x(y). This nullifies the practical advantage of accessor functions, and it remains OOP because the property 'x' becomes a legitimate part of the object's interface: it need not reflect an implementation detail.

In version 2.2 of Python, "new-style" classes were introduced. With new-style classes, objects and types were unified, allowing the subclassing of types. Even new types entirely can be defined, complete with custom behavior for infix operators. This allows for many radical things to be done syntactically within Python, such as the ability to use C++-style input and output. A new multiple inheritance model was adopted with new-style classes, making a much more logical order of inheritance, adopted from Common Lisp. The new method __getattribute__ was also defined for unconditional handling of attribute access.

Exception handling

Python supports (and extensively uses) exception handling as a means of testing for error conditions and other "exceptional" events in a program. Indeed, it is even possible to trap the exception caused by a syntax error!

Exceptions permit more concise and reliable error checking than many other ways of reporting erroneous or exceptional events. Exceptions are thread-safe; they tend not to clutter up code in the way that testing for returned error codes does in C; and they can easily propagate up the calling stack when an error must be reported to a higher level of the program.

Python style calls for the use of exceptions whenever an error condition might arise. Indeed, rather than testing for access to a file or resource before actually using it, it is conventional in Python to just go ahead and try to use it, catching the exception if access is rejected.

Exceptions can also be used as a more general means of non-local transfer of control, even when an error is not at issue. For instance, the Mailman mailing list software, written in Python, uses exceptions to jump out of deeply-nested message-handling logic when a decision has been made to reject a message or hold it for moderator approval.

Exceptions are often, especially in threaded situations, used as an alternative to the if block. A commonly-invoked motto is EAFP, or "It is easier to ask for Forgiveness than to ask for Permission." Consider these two equivalent pieces of code:

try:
  foo.bar
except AttributeError:
  do_something_else(foo)
else:
  do_something(foo)
if hasattr(foo, 'bar'):
  do_something(foo)
else:
  do_something_else(foo)

Standard library

Python comes with "batteries included"
Enlarge
Python comes with "batteries included"

Python has a large standard library, which makes it well suited to many tasks. This comes from a so-called "batteries included" philosophy for Python modules. The modules of the standard library can be augmented with custom modules written in either C or Python. The standard library is particularly well tailored to writing Internet-facing applications, with a large number of standard formats and protocols (such as MIME and HTTP) supported. Modules for creating graphical user interfaces, connecting to relational databases, and manipulating regular expressions are also included.

The standard library is one of Python's greatest strengths. The bulk of it is cross-platform compatible, meaning that even heavily leveraged Python programs can often run on Unix, Windows, Macintosh, and other platforms without change.

It is currently being debated whether or not third-party but open source Python modules such as Twisted Matrix, NumPy, or wxPython should be included in the standard library, in accordance with the batteries included philosophy.

Other features

Like Lisp, and unlike Perl, the Python interpreter also supports an interactive mode in which expressions can be entered from the terminal and results seen immediately. This is a boon for those learning the language and experienced developers alike: snippets of code can be tested in interactive mode before integrating them into a program proper.

Python also includes a unit testing framework for creating exhaustive test suites. While static typing aficionados see this as a replacement for a static type-checking system, Python programmers largely do not share this view.

Finally, Python supports continuations for manipulating control flow.

Neologisms

A few neologisms have come into common use within the Python community. One of the most common is "pythonic", which can have a wide range of meanings related to program style. To say that a piece of code is pythonic is to say that it uses Python idioms well; that it is natural or shows fluency in the language. Likewise, to say of an interface or language feature that it is pythonic is to say that it works well with Python idioms; that its use meshes well with the rest of the language.

In contrast, a mark of unpythonic code is that it attempts to "write C++ (or Lisp, or Perl) code in Python"—that is, provides a rough transcription rather than an idiomatic translation of forms from another language.

The prefix Py- can be used to show that something is related to Python, much as a prefixed J- denotes Java. Examples of the use of this prefix in names of Python applications or libraries include PyGame, a binding of SDL to Python; PyUI, a GUI encoded entirely in Python; PySol, a series of card games programmed in Python; and PyAlaMode, an IDE for Python created by Orbtech, a company specializing in Python.

Supported platforms

Although Python was originally programmed for the Amoeba platform, that version is "dead" (i.e. it hasn't been updated in a while). The most popular (and therefore best maintained) platforms Python runs on are Microsoft Windows, Linux, BSD, Mac OS X, and Java (the Java version being a completely separate implementation). A Mac GUI on Python is maintained by an external project called MacPython, and was included in Mac OS 10.3 "Panther". Other supported platforms include:

Unfortunately, most of the third-party libraries for Python (and even some first-party ones) are only available on Windows, Linux, BSD, and Mac OS X.

Prominent Python software

Notable Python-related software includes:

Software written in Python

  • Google, a web searching engine
  • Zope, an object-oriented web-application platform. Zope includes an application server with an integrated object-oriented database and a built-in web-based management interface.
  • The original BitTorrent implementation, and several derivatives.
  • Portage, the heart of Gentoo Linux. An advanced package management system based on the *BSD style ports system.
  • Mailman, one of the more popular packages for running email mailing lists.
  • Solipsis, a system for massively shared virtual world.
  • ViewCVS, a web-based interface for browsing CVS repositories

Packages for Python

  • mod_python, an Apache module allowing direct integration of Python scripts with the Apache web server.
  • Pygame http://www.pygame.org Python game development
  • wxPython (http://www.wxpython.org/), a port of wxWidgets and a popular cross-platform GUI library for Python
  • PyGTK, http://www.pygtk.org/, a popular cross-platform GUI library based on GTK+
  • PyQt (http://www.riverbankcomputing.co.uk/pyqt/), another popular cross-platform GUI library based on Qt
  • SciPy, a library of scientific and numerical routines
  • Twisted (http://www.twistedmatrix.com/), a networking framework for Python
  • ZODB (http://zope.org/Wikis/ZODB/FrontPage/guide/zodb.html) a Python-specific object-oriented database

Miscellaneous

External links and references

Wikibooks
Wikibooks Programming has more about this subject:
Major programming languages (more)

Ada | ALGOL | APL | AWK | BASIC | C | C++ | C# | COBOL | Delphi | Eiffel | Fortran | Haskell | IDL | Java | JavaScript | Lisp | LOGO | ML | Objective-C | Pascal | Perl | PHP | PL/I | Prolog | Python | Ruby | SAS | Scheme | sh | Simula | Smalltalk | SQL | Visual Basic


Example Usage of (language)

WeLoveProject: New post: The Bass-ic Language Of A Bass Guitar (http://cli.gs/eMRTB) http://cli.gs/eMRTB
LocavoreBlog: Copenhagen Agreement on Agriculture Draft Language: Here's the actual draft language on ag from the Copenhagen agre... http://bit.ly/4VubI7
heff1007: the English language has more words than any other yet it still sometimes isn't enough to find the right ones
Copyright 2009 WordIQ.com - Privacy Policy  :: Terms of Use  :: Contact Us  :: About Us
This article is licensed under the GNU Free Documentation License. It uses material from the this Wikipedia article.