All Topics  
Python syntax and semantics

 

   Email Print
   Bookmark   Link






 

Python syntax and semantics



 
 
The syntax of the Python programming language
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
 is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers). Python was designed to be a highly readable language. It has a relatively uncluttered visual layout, uses English keywords frequently where other languages use punctuation, and has notably fewer syntactic constructions than other popular structured languages such as C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, or PHP
PHP

PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in Standalone software Graphical user interface....
.

on uses whitespace
Whitespace (computer science)

In computer science, whitespace is any single character or series of characters that represents horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visual mark, but typically does occupy an area on a page....
 to delimit program blocks, following the off-side rule
Off-side rule

A computer programming language is said to adhere to the off-side rule if in it the scope of declarations is expressed by their indentation, i.e., blocks are formed via indentation....
.






Discussion
Ask a question about 'Python syntax and semantics'
Start a new discussion about 'Python syntax and semantics'
Answer questions from other users
Full Discussion Forum



Encyclopedia


The syntax of the Python programming language
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
 is the set of rules that defines how a Python program will be written and interpreted (by both the runtime system and by human readers). Python was designed to be a highly readable language. It has a relatively uncluttered visual layout, uses English keywords frequently where other languages use punctuation, and has notably fewer syntactic constructions than other popular structured languages such as C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, or PHP
PHP

PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in Standalone software Graphical user interface....
.

Indentation

Python uses whitespace
Whitespace (computer science)

In computer science, whitespace is any single character or series of characters that represents horizontal or vertical space in typography. When rendered, a whitespace character does not correspond to a visual mark, but typically does occupy an area on a page....
 to delimit program blocks, following the off-side rule
Off-side rule

A computer programming language is said to adhere to the off-side rule if in it the scope of declarations is expressed by their indentation, i.e., blocks are formed via indentation....
. Its uncommon block marking convention is a feature that many programmers otherwise unfamiliar with Python have heard of. Python borrows a feature from its predecessor ABC—instead of punctuation or keywords, it uses indentation to indicate the run of a block.

In so-called "free-format" languages, that use the block structure derived from ALGOL
Algol

Algol , known colloquially as the Demon Star, is a bright star in the constellation Perseus . It is one of the best known eclipsing binary, the first such star to be discovered, and also one of the first variable stars to be discovered....
, blocks of code are set off with braces () or keywords. In most coding conventions for these languages programmers conventionally indent the code within a block, to set it off visually from the surrounding code.

Consider a function, foo, which is passed a single parameter
Parameter

In mathematics, statistics, and the mathematical sciences, a parameter is a quantity that defines certain characteristics of systems or function s....
, x, and if the parameter is 0 will call bar and baz, otherwise it will call qux, passing x, and also call itself recursively, passing x-1 as the parameter. Here are implementations of this function in both C and Python:

foo function in C with K&R indent style
Indent style

In computer programming, an indent style is a convention governing the indentation of block s of code to convey the program's structure. This article largely addresses the C and its descendants, but can be applied to most other programming languages ....
:

void foo(int x)

foo function in Python: def foo(x): if x

0: bar baz else: qux(x) foo(x - 1)

Python mandates a convention that programmers in ALGOL-style languages often follow. Moreover, in free-form syntax, since indentation is ignored, good indentation cannot be enforced by an interpreter or compiler. Incorrectly indented code can be understood by human reader differently than does a compiler or interpreter. For example:

Misleading indentation in C: for (i = 0; i < 20; ++i) a; b; c;

This code is intended to call functions a, b, and c 20 times. However, the interpreted code block is just . The code calls a 20 times, and then calls b and c one time each. Novice Python programmers frequently misread samples like this.

Both space characters and tab characters are currently accepted as forms of indentation in Python. Since many tools do not visually distinguish them, mixing spaces and tabs can create bugs that take specific efforts to find (a perennial suggestion among Python users has been removing tabs as block markers—except, of course, among those Python users who propound removing spaces instead). Moreover, formatting routines which remove whitespace—for instance, many Internet forum
Internet forum

An , or 'message board', is an online discussion site. It is the modern equivalent of a traditional bulletin board, and a technological evolution of the dialup bulletin board system....
s—can completely destroy the syntax of a Python program, whereas a program in a bracketed language would merely become more difficult to read.

Because whitespace is syntactically significant, it is not generally possible for a text editor or code prettifier to automatically correct the indentation in Python code as can be done with C or Lisp
Lisp programming language

Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older....
 code. Many popular code editors handle Python's indentation conventions seamlessly, sometimes after a configuration option is enabled.

Data structures

Since Python is a dynamically typed language, Python values, not variables, carry type. This has implications for many aspects of the way the language functions.

All variables in Python hold references to objects, and these references are passed to functions; a function cannot change the value a variable references in its calling function. Some people (including Guido van Rossum
Guido van Rossum

Guido van Rossum is a Netherlands computer programmer who is best known as the author of the Python . In the Python community, Van Rossum is known as a ?Benevolent Dictator for Life? , meaning that he continues to oversee the Python development process, making decisions where necessary....
 himself) have called this parameter-passing scheme "Call by object reference."

Among dynamically typed languages, Python is moderately type-checked. 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; a string is an invalid argument to a mathematical function expecting a number.

Base types

Python has a broad range of basic data types. Alongside conventional integer and floating point arithmetic, it transparently supports arbitrary-precision arithmetic
Arbitrary-precision arithmetic

In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, is a technique whereby computer programs perform calculations on integers or rational numbers with an arbitrary number of numerical digits of precision , typically limited only by the available memory of the host system....
, complex number
Complex number

In mathematics, the complex numbers are an extension of the real numbers obtained by adjoining an imaginary unit, denoted i, which satisfies:...
s, and decimal floating point numbers
Decimal floating point

Decimal floating point arithmetic refers to both a representation and operations on decimal floating point numbers. Working directly with base 10 exponents can avoid rounding errors that result in conversion to base 2 exponents....
.

Python supports a wide variety of string operations. Strings in Python are immutable
Immutable object

In object-oriented computer programming and Functional_programming programming, an immutable object is an object whose state cannot be modified after it is created....
, so a string operation such as a substitution of characters, that in other programming languages might alter a string in place, returns a new string in Python. Performance considerations sometimes push for using special techniques in programs that modify strings intensively, such as joining character arrays into strings only as needed.

Collection types

One of the very useful 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: sequences and mappings.

The ordered sequential types are lists (dynamic arrays), tuple
Tuple

In mathematics, a tuple is a sequence of a specific number of values, called the components of the tuple. These components can be any kind of mathematical objects, where each component of a tuple is a value of a specified type....
s, and strings. All sequences are indexed positionally (0 through length − 1) and all but strings can contain any type of object, including multiple types in the same sequence. Both strings and tuples are immutable, making them perfect candidates for dictionary keys (see below). Lists, on the other hand, are mutable; elements can be inserted, deleted, modified, appended, or sorted in-place
In-place algorithm

In computer science, an in-place algorithm is an algorithm which transforms a data structure using a small, constant amount of extra storage space....
.

On the other side of the collections coin are mappings, which are unordered types implemented in the form of dictionaries which "map" a set of immutable keys, to corresponding elements much like a mathematical function. The keys in a dictionary must be of an immutable Python type such as an integer or a string. For example, one could define a dictionary having a string "toast" mapped to the integer 42 or vice versa. This is done under the covers via a hash function
Hash function

A hash function is any algorithm or function which converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an array index into an array....
 which makes for faster lookup times, but is also the culprit for a dictionary's lack of order and is the reason mutable objects (i.e. other dictionaries or lists) cannot be used as keys. Dictionaries are also central to the internals of the language as they reside at the core of all Python objects and classes: the mapping between variable names (strings) and the values which the names reference is stored as a dictionary (see Object system
Python syntax and semantics

The syntax of the Python is the set of rules that defines how a Python program will be written and interpreted . Python was designed to be a highly readable language....
). Since these dictionaries are directly accessible (via an object's __dict__ attribute), metaprogramming
Metaprogramming

Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at runtime that would otherwise be done at compile time....
 is a straightforward and natural process in Python.

A set
Set (computer science)

In computer science, a set is a collection of certain values, without any particular Canonical order, and no repeated values. It corresponds with a finite set in mathematics....
 collection type was added to the core language in version 2.4. A set is an unindexed, unordered collection that contains no duplicates, and implements set theoretic
Naive set theory

Naive set theory is one of several theories of sets used in the discussion of the foundations of mathematics. The informal content of this naive set theory supports both the aspects of mathematical sets familiar in discrete mathematics , and the everyday usage of set theory concepts in most contemporary mathematics....
 operations such as union
Union (set theory)

In set theory, the term Union refers to a set operation used in the convergence of set elements to form a resultant set containing the elements of both sets....
, intersection
Intersection (set theory)

In mathematics, the intersection of two Set A and B is the set that contains all elements of A that also belong to B , but no other elements....
, difference
Complement (set theory)

In discrete mathematics and predominantly in set theory, a complement is a concept used in comparisons of sets to refer to the unique values of one set in relation to another....
, symmetric difference
Symmetric difference

In mathematics, the symmetric difference of two Set is the set of elements which are in one of the sets, but not in both. This operation is the set-theoretic kin of the exclusive disjunction in Boolean logic....
, and subset
Subset

In mathematics, especially in set theory, a Set A is a subset of a set B if A is "contained" inside B. Notice that A and B may coincide....
 testing. There are two types of sets: set and frozenset, the only difference being that set is mutable and frozenset is immutable. Elements in a set must be hashable and immutable. Thus, for example, a frozenset can be an element of a regular set whereas the opposite is not true.

Python also provides extensive collection manipulating abilities such as built in containment checking and a generic iteration protocol.

Object system

In Python, everything is an object, even classes. Classes, as objects, have a class, which is known as their metaclass
Metaclass

In object-oriented programming Computer programming, a metaclass is a Class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances....
. Python also supports multiple inheritance
Multiple inheritance

Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
 and mixin
Mixin

In object-oriented programming languages, a mixin is a class that provides a certain functionality to be Inheritance by a subclass, but is not meant to stand alone....
s (see also MixinsForPython).

The language supports extensive introspection of types and classes. Types can be read and compared—types are instances of 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.

Operators


Comparison operators

The basic comparison operators such as , <, >=, and so forth, are used on all manner of values. Numbers, strings, sequences, and mappings can all be compared. Although disparate types (such as a str and a int) are defined to have a consistent relative ordering, this is considered a historical design quirk, and will no longer be allowed in Python 3000.

Chained comparison expressions such as a < b < c have roughly the meaning that they have in mathematics, rather than the unusual meaning found in C and similar languages. The terms are evaluated and compared in order. The operation has short-circuit
Minimal evaluation

Short-circuit evaluation or minimal evaluation denotes the semantics of some boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and evaluates to false
 semantics, meaning that evaluation is guaranteed to stop as soon as a verdict is clear: if a < b is false, c is never evaluated as the expression cannot possibly be true anymore.

For expressions without side effects, a < b < c is equivalent to a < b and b < c. However, there is a substantial difference when the expressions have side effects. a < f(x) < b will evaluate f(x) exactly once, whereas a < f(x) and f(x) < b will evaluate it twice if the value of a is less than f(x) and once otherwise.

Logical operators

Python 2.2 and earlier does not have an explicit boolean type. In all versions of Python, boolean operators treat zero values or empty values such as "", 0, None, 0.0, [], and as false, while in general treating non-empty, non-zero values as true. In Python 2.2.1 the boolean constants True and False were added to the language (subclassed from 1 and 0). The binary comparison operators such as

and > return either True or False.

The boolean operators and and or use minimal evaluation
Minimal evaluation

Short-circuit evaluation or minimal evaluation denotes the semantics of some boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: when the first argument of and evaluates to false
. For example, y

0 or x/y > 100 will never raise a divide-by-zero exception. Note that these operators return the value of the last operand evaluated, rather than True or False. Thus the expression (4 and 5) evaluates to 5, and (4 or 5) evaluates to 4.

Functional programming

As mentioned above, another strength of Python is the availability of a functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
 style. As may be expected, this makes working with lists and other collections much more straightforward.

List comprehensions

One such construction is the list comprehension
List comprehension

A list comprehension is a Syntax of programming languages construct available in some programming languages for creating a list based on existing lists....
, as seen here in calculating the first five powers of two:

powers_of_two = [2**n for n in xrange(1, 6)]

The Quicksort
Quicksort

Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, average performance, makes comparisons to sort n items. However, in the Best, worst and average case, it makes comparisons....
 algorithm can be expressed elegantly using list comprehensions:

def qsort(L): if L

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

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

First-class functions

In Python, functions are first-class
First-class function

In computer science, a programming language is said to support first-class functions if it treats function s as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning the...
 objects that can be created and passed around dynamically.

Python's limited support for anonymous function
Anonymous function

In computing, an anonymous function is a function defined, and possibly called, without being bound to a name. In lambda calculus, all functions are anonymous....
s is the lambda construct. Since the availability of full anonymous functions is non-existent then named functions is the primary use of functions in Python. Lambdas are limited to containing expressions rather than statements, although control flow can still be implemented less elegantly within lambda by using short-circuiting.

Closures

Python has had support for lexical closures
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
 since version 2.2. Here's an example: def derivative(f, dx): """Return a function that approximates the derivative of f using an interval of dx, which should be appropriately small. """ def function(x): return (f(x + dx) - f(x)) / dx return function

Python's syntax, though, sometimes leads programmers of other languages to think that closures are not supported. Variable scope in Python is implicitly determined by the scope in which one assigns a value to the variable, so it's not possible to assign to a variable from an outer scope without an explicit declaration. The following example illustrates the use of the nonlocal keyword to declare the scope of a variable in a nested function:

def makeCounter: num = 0 def count(x): nonlocal num num += 1 return num return count

Generators

Introduced in Python 2.2 as an optional feature and finalized in version 2.3, generators
Generator (computer science)

In computer science, a generator is a special subroutine that can be used to control the iteration behaviour of a control flow#Loops. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values....
 are Python's mechanism for lazy evaluation
Lazy evaluation

In computer programming, lazy evaluation is the technique of delaying a computation until such time as the result of the computation is known to be needed....
 of a function that would otherwise return a space-prohibitive or computationally intensive list.

This is an example to lazily generate the prime numbers:

from itertools import count

def generate_primes(stop_at=0): primes = [] for n in count(2): if 0 < stop_at < n: return # raises the StopIteration exception composite = False for p in primes: if not n % p: composite = True break elif p**2 > n: break if not composite: primes.append(n) yield n

To use this function simply call, e.g.:

for i in generate_primes: # iterate over ALL primes if i > 100: break print i

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 scope. 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.

Generators don't have to be infinite like the prime-number example above. When a generator terminates, an internal exception is raised which indicates to any calling context that there are no more values. A for loop or other iteration will then terminate.

Generator expressions

Introduced in Python 2.4, generator expressions are the lazy evaluation equivalent of list comprehensions. Using the prime number generator provided in the above section, we might define a lazy, but not quite infinite collection.

from itertools import islice

first_million_primes = (i for i in generate_primes if i < 1000000) two_thousandth_prime = islice(first_million_primes, 2000, 2001).next

Most of the memory and time needed to generate this many primes will not be used until the needed element is actually accessed. Unfortunately, you cannot perform simple indexing and slicing of generators, but must use the itertools modules or "roll your own" loops. In contrast, a list comprehension is functionally equivalent, but is greedy in performing all the work:

first_million_primes = [i for i in generate_primes(2000000) if i < 1000000] two_thousandth_prime = first_million_primes[1999]

The list comprehension will immediately create a large list (with 78498 items, in the example, but transiently creating a list of primes under two million), even if most elements are never accessed. The generator comprehension is more parsimonious.

Dict and set comprehensions

While lists and generators had comprehensions/expressions, in Python 2.x the other Python built-in collection types (dicts and sets) had to be kludged in using lists or generators:

>>> dict((n, n*n) for n in range(5))

Python 3.0 unifies all collection types by introducing dict and set comprehensions, similar to list comprehensions:

>>> [ n*n for n in range(5) ] # regular list comprehension [0, 1, 4, 9, 16] >>> >>> # set comprehension >>> >>> # dict comprehension

Objects

Python supports most object oriented programming techniques. It allows polymorphism, not only within a class hierarchy
Class hierarchy

As in taxonomy, the classifications of species, a class hierarchy in computer science is a classification of object types, denoting objects as the instantiations of classes inter-relating the various classes by relationships such as "inherits", "extends", "is an abstraction of", "an interface definition"....
 but also by duck typing
Duck typing

In computer programming, duck typing is a style of dynamic typing in which an object's current set of Method s and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface....
. Any object can be used for any type, and it will work so long as it has the proper methods and attributes. And everything in Python is an object, including classes, functions, numbers and modules. Python also has support for metaclass
Metaclass

In object-oriented programming Computer programming, a metaclass is a Class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances....
es, an advanced tool for enhancing classes' functionality. Naturally, inheritance
Inheritance (computer science)

In object-oriented programming, inheritance is a way to form new class es using classes that have already been defined. The inheritance concept was invented in 1967 for Simula....
, including multiple inheritance
Multiple inheritance

Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
, is supported. It has limited support for private variables using name mangling
Name mangling

In software compiler software engineering, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....
. See 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
Information hiding

Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed....
 to be unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals. However, the strongest argument for name mangling is prevention of unpredictable breakage of programs: introducing a new public variable in a superclass can break subclasses if they don't use "private" variables.

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, it offers an object system but does not demand OOP behavior. 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 spam.eggs = toast might really invoke spam.set_eggs(toast). 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 entirely new types can be defined, complete with custom behavior for infix operators. This allows for many radical things to be done syntactically within Python. A new for multiple inheritance
Multiple inheritance

Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
 was also adopted with Python 2.3. It is also possible to run custom code while accessing or setting attributes, though the details of those techniques have evolved between Python versions.

Exceptions

Python supports (and extensively uses) exception handling
Exception handling

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions - special conditions that change the normal flow of execution....
 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
Syntax error

In computer science a syntax error refers to an error in the syntax of a sequence of characters or token that is intended to be written in a particular programming language....
.

Python style calls for the use of exceptions whenever an error condition might arise. 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
GNU Mailman

GNU Mailman is a computer software application from the GNU project for managing electronic mailing lists.Mailman is coded primarily in Python and currently maintained by Barry Warsaw....
 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
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
 situations, used as an alternative to the if-block. A commonly-invoked motto is EAFP, or "It is Easier to Ask for Forgiveness than Permission." In this first code sample, there is an explicit check for the attribute (i.e., "asks permission"):

if hasattr(spam, 'eggs'): ham = spam.eggs else: handle_error

This second sample follows the EAFP paradigm:

try: ham = spam.eggs except AttributeError: handle_error

These two code samples have the same effect, although there will be performance differences. When spam has the attribute eggs, the EAFP sample will run faster. When spam does not have the attribute eggs (the "exceptional" case), the EAFP sample will run slower. The Python can be used in specific cases to determine performance characteristics. If exceptional cases are rare, then the EAFP version will have superior average performance than the alternative. In addition, it avoids the whole class of time-of-check-to-time-of-use
Time-of-check-to-time-of-use

A time-of-check-to-time-of-use bug is a software bug caused by changes in a system between the checking of a condition and the use of the results of that check....
 (TOCTTOU) vulnerabilities, other race conditions, and is compatible with Duck Typing
Duck typing

In computer programming, duck typing is a style of dynamic typing in which an object's current set of Method s and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface....
.

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 docstring
Docstring

In programming, a docstring is a string literal specified in source code that is used, like a comment , to document a specific segment of code. Unlike conventional source code comments, or even specifically formatted comments like Javadoc documentation, docstrings are not stripped from the source tree when it is parsed, but are retained thro...
s (documentation string), that is a string that is located alone without assignment as the first line within a module, class, method or function. Such strings can be delimited with " or ' for single line strings, or may span multiple lines if delimited with either """ or which is Python's notation for specifying multi-line strings. However, the style guide for the language specifies that triple double quotes (""") are preferred for both single and multi-line docstrings.

Single line docstring: def getline: """Get one line from stdin and return it.""" return sys.stdin.readline

Multi-line docstring: 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 break
Line break

Line break may refer to:*Line break , a literary device*Soft return, a wrapping of a line of text*Newline, an indicator of the end of a line of text...
s. In contrast with comments, docstrings are themselves Python objects and are part of the interpreted code that Python runs. That means that a running program can retrieve its own docstrings and manipulate that information. But the normal usage is to give other programmers information about how to invoke the object being documented in the docstring.

There are tools available that can extract the docstrings to generate an API
Application programming interface

An application programming interface is a set of subroutine, data structures, class and/or Protocol provided by library and/or operating system Service s in order to support the building of applications....
 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.

The doctest
Doctest

doctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings....
 standard module uses interactions copied from Python shell sessions into docstrings, to create tests.

Decorators

A decorator is a Python object that can be called with a single argument, and that modifies functions or methods. Python decorators were inspired in part by Java annotation
Java annotation

An annotation, in the Java , is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated....
s, and have a similar syntax; the decorator syntax is pure syntactic sugar
Syntactic sugar

Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its Function but make it "sweeter" for humans to use....
, using @ as the keyword:

@viking_chorus def menu_item: print "spam"

is equivalent to

def menu_item: print "spam" menu_item = viking_chorus(menu_item)

Decorators are a form of metaprogramming
Metaprogramming

Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at runtime that would otherwise be done at compile time....
; they enhance the action of the function or method they decorate. For example, in the above sample, viking_chorus might cause menu_item to be run 8 times for each time it is called:

def viking_chorus(myfunc): def inner_func(*args, **kwargs): for i in range(8): myfunc(*args, **kwargs) return inner_func

Canonical uses of function decorators are for creating class methods or static methods, adding function attributes, tracing
Tracing (software)

In software engineering, tracing is a specialized use of Data logging to record information about a program's execution. This information is typically used by programmers for debugging purposes, and additionally, depending on the type and detail of information contained in a trace log, by experienced system administrators or technical suppor...
, setting pre-
Precondition

In computer programming, a precondition is a condition or Predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification....
 and postcondition
Postcondition

In computer programming, a postcondition is a condition or Predicate that must always be true just after the execution of some section of code or after an operation in a formal specification....
s, and synchronisation, but can be used for far more besides, including tail recursion elimination, memoization
Memoization

In computing, memoization is an Optimization technique used primarily to speed up computer programs by having Subroutine avoid repeating the calculation of results for previously-processed inputs....
 and even improving the writing of decorators.

Decorators can be chained by placing several on adjacent lines:

@invincible @favourite_colour("Blue") def black_knight: pass

is equivalent to

def black_knight: pass black_knight = invincible(favourite_colour("Blue")(black_knight))

In the above example, the favourite_colour decorator can take an argument (or arguments depending on its function definition). Decorator functions that do that must return yet another decorator that takes one argument, the function to be decorated: def favourite_colour(colour): def yet_another_decorator(func): def wrapper: print colour func return wrapper return yet_another_decorator This would then decorate the black_knight function such that the color, "Blue", would be printed prior to the black_knight function running.

In Python prior to version 3, decorators apply to functions and methods, but not to classes. Decorating a (dummy) __new__ method can modify a class, however. Class decorators are supported in Python 3.0.

Despite the name, Python decorators are not an implementation of the decorator pattern
Decorator pattern

In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing Class dynamically....
. The decorator pattern is a design pattern
Design pattern

A design pattern in architecture and computer science is a formal way of documenting a solution to a design problem in a particular field of expertise....
 used in statically typed object-oriented programming language
Object-oriented programming language

An object-oriented programming language is one that allows or encourages, to some degree, object-oriented programming techniques such as Information hiding, Inheritance , module , and Polymorphism ....
s to allow functionality to be added to objects at run time; Python decorators add functionality to functions and methods at definition time, and thus are a higher-level construct than decorator-pattern classes. The decorator pattern itself is trivially implementable in Python, because the language is duck typed, and so is not usually considered as such.

Easter Eggs



Users of curly bracket programming language
Curly bracket programming language

Curly brace or bracket programming languages are those which use balanced brackets to make block s in their syntax or formal grammar, mainly due to being C -influenced....
s, such as C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 or Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, sometimes expect or wish Python to follow a block-delimiter convention. Brace-delimited block syntax has been repeatedly requested, and consistently rejected by core developers. The Python interpreter contains an easter egg that summarizes its developers' feelings on this issue. The code from __future__ import braces raises the exception SyntaxError: not a chance.

Another hidden message, The Zen of Python (a summary of Python philosophy), is displayed when trying to import this.

An antigravity module was added to Python 3.0. Importing the module opens a web browser to an xkcd
Xkcd

xkcd is a webcomic created by Randall Munroe, a former Independent contractor for NASA. Munroe describes it as "a webcomic of romance, sarcasm, math, and language." xkcd is a widely read webcomic and has been recognized in mainstream media such as The Guardian....
 comic that portrays Python having an antigravity module.

External links

  • - Tutorial written by the author of Python, Guido van Rossum.