Differences between revisions 1 and 31 (spanning 30 versions)
Revision 1 as of 2007-03-19 14:03:06
Size: 3871
Editor: PaulBoddie
Comment: A topic not covered very well, as far as I've seen. May be mistaken...
Revision 31 as of 2023-09-17 14:01:23
Size: 17058
Editor: MatsWichmann
Comment: eidtorial, markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= How To Edit Python Code = <<TableOfContents>>
= History =
Python was conceived in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI in Amsterdam. Python is a successor to the ABC programming language (itself inspired by SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum is Python's principal author, and was for many years known by the humorous title ''Benevolent Dictator for Life'' (BDFL). Python evolution is now guided by an elected Steering Committee.
-----
= Programming philosophy =
Line 3: Line 7:
Python uses an indentation-based syntax for grouping statements into blocks. For example: Python is a multi-paradigm programming language. Rather than forcing programmers to adopt a particular style of programming, it permits several styles: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming (including metaprogramming and "by magic" methods). Many other paradigms are supported using extensions, such as pyDBC and Contracts for Python which allow Design by Contract.
Line 5: Line 9:
{{{
if some_condition and some_other_condition:
    do_something()
do_something_else()
}}}
Rather than requiring all desired functionality to be built into the language's core, Python was designed to be highly extensible. New built-in modules can be easily written in C, C++ or Cython. Python can also be used as an extension language for existing modules and applications that need a programmable interface. This design, a small core language with a large standard library with an easily extensible interpreter, was intended by Van Rossum from the very start because of his frustrations with ABC (which espoused the opposite mindset).
Line 11: Line 11:
However, beginners to Python programming are sometimes worried about this aspect of the language, asking questions like... -----
Line 13: Line 13:
 * How can I be sure that Python won't change its mind about my indentation?
 * What happens if someone edits my code and mixes in tabs and spaces?
 * Isn't Python susceptible to problems I've had with Makefiles?
= Name and neologisms =
Line 17: Line 15:
Whilst there is always a risk that someone with a badly behaved editor will start mixing tabs and spaces, potentially confusing Python, it should be remembered that such editing habits will probably confuse ''programmers'' working with such code in any language, as is shown by the following classic example in C-like languages: An important goal of the Python developers is making Python fun to use. This is reflected in the origin of the name (based on 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.[24][25] For example, the metasyntactic variables often used in Python literature are spam and eggs, instead of the traditional foo and bar.
Line 19: Line 17:
{{{
if (some_condition && some_other_condition)
    do_something();
    do_something_else();
}}}
-----
Line 25: Line 19:
Although a C or C++ compiler will accept something like this, there may be a clear mismatch between what the programmer intended and the behaviour of the resulting code. Clearly, "sloppy" editing habits have an impact regardless of the role of indentation in a language's syntax. = Usage =
Line 27: Line 21:
== Choosing a Text Editor == Python is often used as a scripting language for web applications, e.g. via mod_wsgi for the Apache web server. With Web Server Gateway Interface, a standard API has been developed to facilitate these applications. Web application frameworks like Django, Pylons, TurboGears, web2py, Flask, and Zope support developers in the design and maintenance of complex applications. Libraries like NumPy, SciPy, and Matplotlib allow Python to be used effectively in scientific computing.
Line 29: Line 23:
You may have a favourite text editor in which you want to use to edit Python code. Hopefully, this editor will be slightly more sophisticated than the Notepad editor (on Windows) and will let you change its settings so that Python code can be edited effectively. Many text editors have support for syntax highlighting (so that different aspects of the text have different colours - keywords might be yellow, strings might be purple, and so on), and such things can be worthwhile benefits which can tempt you away from more primitive (if more familiar) programs. A list of Python-compatible editors can be found on the PythonEditors page. -----
Line 31: Line 25:
== Configuring a Text Editor == = Syntax and semantics =
Line 33: Line 27:
Now that you've made a choice from the PythonEditors list, you'll want to make sure that it is set up properly. Below you'll find a guide for a number of different editors dealing with important Python issues such as indentation (tabs and spaces) as well as other more general issues like encodings and syntax highlighting/colouring. Python was intended to be a highly readable language. It is designed to have an uncluttered visual layout, frequently using English keywords where other languages use punctuation. Python requires less boilerplate than traditional manifestly typed structured languages such as C or Pascal, and has a smaller number of syntactic exceptions and special cases than either of these. For a detailed description of the differences between 2.x and 3.x versions, see History of Python.
Line 35: Line 29:
---- == Indentation ==
Line 37: Line 31:
== Vim (and relatives) == Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks (a feature also known as the ''off-side rule''). An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.
Line 39: Line 33:
See: http://www.vim.org/ == Statements and control flow ==
Line 41: Line 35:
=== Indentation === Python's statements include (among others):
Line 43: Line 37:
A useful addition to Python source files is this comment:  *The '''''if''''' statement, which conditionally executes a block of code, along with '''''else''''' and '''''elif''''' (a contraction of else-if).
 *The '''''for''''' statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
 *The '''''while''''' statement, which executes a block of code as long as its condition is true.
 *The '''''try''''' statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses; it also ensures that clean-up code in a '''''finally''''' block will always be run regardless of how the block exits.
 *The '''''class''''' statement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming.
 *The '''''def''''' statement, which defines a function or method.
 *The '''''with''''' statement (from Python 2.5), which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run, and releasing the lock afterward).
 *The '''''pass''''' statement, which serves as a NOP and can be used in place of a code block.
 *The '''''assert''''' statement, used during debugging to check for conditions that ought to apply.
 *The '''''yield''''' statement, which returns a value from a generator function. (From Python 2.5, '''''yield''''' is also an operator. This form is used to implement coroutines -- see below.)
Line 45: Line 48:
{{{
# vim: tabstop=4 expandtab shiftwidth=4
}}}
== Expressions ==
Line 49: Line 50:
This tells Vim that when the file is loaded, tabs are always expanded to spaces and that the width of each tab is four characters. Type the following in command mode to achieve the same effect:  *In Python 3, the result of the division operator `/` with integer operands is always a floating-point value; the operator `//` may be used to perform integer division (the result truncated to an integer).
  *In Python 2, the `/` operator on integers performs integer division. Floating-point division on integers can be achieved by converting one of the integers to a float (e.g. `float(x) / y`), or, since 2.2, the behavior of Python 3 can be enabled using {{{from __future__ import division}}}.
 *In Python, `==` compares by value, in contrast to Java, where it compares by reference. (Value comparisons in Java use the equals() method.) Python's `is` operator may be used to compare object identities (comparison by reference). Comparisons may be chained, for example `a <= b <= c`.
 *Python uses the words '''and''', '''or''', '''not''' for its boolean operators rather than the symbolic `&&`, `||`, `!` used in C.
 *Python has a type of expression known as a list comprehension. Python 2.4 extended list comprehensions into a more general expression known as a generator expression.
 *Anonymous functions are implemented using lambda expressions; however, these are limited in that the body can only be a single expression.
 *Conditional expressions in Python are written as `x if c else y` (different in order of operands from the ?: operator common to many other languages).
 *Python makes a distinction between lists and tuples. Lists, written as `[1, 2, 3]`, are mutable, and cannot be used as the keys of dictionaries (dictionary keys must be immutable in Python). Tuples, written as `(1, 2, 3)`, are immutable and thus can be used as the keys of dictionaries, provided all elements of the tuple are immutable. The parentheses around the tuple are optional in some contexts. Tuples can appear on the left side of an equal sign; hence a statement like `x, y = y, x` can be used to swap two variables.
 *Multiple string-formatting operations are supported. The original '''%''' operator provided behavior analogous to printf format strings in C, e.g. `foo=%s bar=%d" % ("blah", 2)` evaluates to `foo=blah bar=2`. String templates were introduced in Python 2.4. Python 3.0 brought in the string `format()` method, with syntax like `foo={0} bar={1}".format("blah", 2)`. Formatted string literals ("F-strings") were introduced in 3.6, with syntax like `f'foo={"blah"} bar={2}'`"
 *Python has various kinds of string literals:
   *Strings are delimited by single or double quotation marks. Unlike in Unix shells, Perl and Perl-influenced languages, single quotation marks and double quotation marks function identically. Both kinds of strings use the backslash (`\`) as an escape character and there is no implicit string interpolation such as "$foo".
   *Triple-quoted strings, which begin and end with a series of three single or double quotation marks, may span multiple lines and function like here-documents in shells, Perl and Ruby.
Line 51: Line 63:
{{{
:set tabstop=4 expandtab shiftwidth=4
}}}
== Methods ==
Line 55: Line 65:
Or: Methods on objects are functions attached to the object's class; the syntax instance.method(argument) is, for normal methods and functions, syntactic sugar for Class.method(instance, argument). Python methods have an explicit self parameter to access instance data, in contrast to the implicit self in some other object-oriented programming languages (for example, Java, C++ or Ruby).
Line 57: Line 67:
{{{
:set ts=4 et sw=4
}}}
== Typing ==
Line 61: Line 69:
=== Syntax Highlighting === Python uses duck typing and has typed objects but untyped variable names. Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically typed, Python is strongly typed, forbidding operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.
Line 63: Line 71:
You may be lucky enough to have syntax highlighting already switched on in your version of Vim. If not, edit a `vimrc` file (either `/etc/vimrc` or `.vimrc` in your home directory) and add the following: == Mathematics ==
Line 65: Line 73:
{{{
syntax on
}}}
Python defines the modulus operator so that the result of a % b is in the half-open interval [0,b), where b is a positive integer. When b is negative, the result lies in the interval (b,0]. However, this consequently affects how integer division is defined. To maintain the validity of the equation b * (a // b) + a % b == a, integer division is defined to round towards minus infinity. Therefore 7 // 3 is 2, but (−7) // 3 is −3. This is different from many programming languages, where the result of integer division rounds towards zero, and Python's modulus operator is consequently defined in a way that can return negative numbers.
Line 69: Line 75:
If you use a dark background, this command may help adjust the colours: -----
Line 71: Line 77:
{{{
set background=dark
}}}
= Implementations =
== CPython ==
Line 75: Line 80:
=== A Simple Template ===
Line 77: Line 81:
You could copy the following simple template and save it to a file somewhere. Then, when you need to make a new source file, just copy it to the intended location with a name of your choice. The mainstream Python implementation, known as CPython, is written in C meeting the C89 standard. CPython compiles Python programs into intermediate bytecode,[65] which are then executed by the virtual machine. It is distributed with a large standard library written in a mixture of C and Python. CPython ships in versions for many platforms, including Microsoft Windows and most modern Unix-like systems. CPython was intended from almost its very conception to be cross-platform; its use and development on esoteric platforms such as Amoeba, alongside more conventional ones like Unix and Mac OS, has greatly helped in this regard. Unofficial builds are also available for [[Android]] and iOS.
Line 79: Line 83:
{{{
#!/usr/bin/env python
== Alternative implementations ==
Line 82: Line 85:
"""
Python source code - replace this with a description of the code and write the code below this text.
"""
Jython compiles the Python program into Java byte code, which can then be executed by every Java Virtual Machine implementation. This also enables the use of Java class library functions from the Python program. IronPython follows a similar approach in order to run Python programs on the .NET Common Language Runtime. PyPy is a fast self-hosting implementation of Python, written in Python, that can output several types of bytecode, object code and intermediate languages. There also exist compilers to high-level object languages, with either unrestricted Python, a restricted subset of Python, or a language similar to Python as the source language. PyPy is of this type, compiling RPython to several languages; other examples include Pyjamas compiling to JavaScript; Shed Skin compiling to C++; and Cython and Pyrex compiling to C.
Line 86: Line 87:
# vim: tabstop=4 expandtab shiftwidth=4
}}}
Line 89: Line 88:
---- == Interpretational semantics ==
Line 91: Line 90:
== Editorial Note == Most Python implementations (including CPython) can function as a command-line interpreter, for which the user enters statements sequentially and receives the results immediately. In short, Python acts as a shell. While the semantics of the other modes of execution (bytecode compilation, or compilation to native code) preserve the sequential semantics, they offer a speed boost at the cost of interactivity, so they are usually only used outside of a command-line interaction (e.g., when importing a module).
Line 93: Line 92:
Feel free to add notes about other editors in the section above, following the format already set out (unless you can think of a better one). Try and arrange the editor sections in ascending alphabetical order. Other shells add capabilities beyond those in the basic interpreter, including IDLE and IPython. While generally following the visual style of the Python shell, they implement features like auto-completion, retention of session state, and syntax highlighting.
Line 95: Line 94:
----
CategoryEditors
-----

= Development =

Python's development is conducted largely through the Python Enhancement Proposal (PEP) process, described in [[https://www.python.org/dev/peps/pep-0001/|PEP 1]]. PEPs are standardized design documents providing general information related to Python, including proposals, descriptions, design rationales, and explanations for language features. Outstanding PEPs are reviewed and commented upon on the python-dev mailing list, which is the primary forum for discussion about the language's development and approved by the Steering Council (see [[https://www.python.org/dev/peps/pep-013|PEP 13]] for the governance model); specific issues are discussed in the bug tracker maintained at [[https://bugs.python.org|bugs.python.org]]. Development of the reference implementation takes place on the [[https://github.com/python/cpython|GitHub cpython]] repository.

CPython's public releases come in three types, distinguished by which part of the version number is incremented:

 * backward-incompatible versions, where code is expected to break and must be manually ported. The first part of the version number is incremented. These releases happen infrequently—for example, version 3.0 was released 8 years after 2.0.
 *major or 'feature' releases, which are largely compatible but introduce new features. The second part of the version number is incremented. These releases are scheduled to occur roughly every 18 months, and each major version is supported by bugfixes for several years after its release.
 *bugfix releases, which introduce no new features but fix bugs. The third and final part of the version number is incremented. These releases are made whenever a sufficient number of bugs have been fixed upstream since the last release, or roughly every 3 months. Security vulnerabilities are also patched in bugfix releases.

A number of alpha, beta, and release-candidates are also released as previews and for testing before the final release is made. Although there is a rough schedule for each release, this is often pushed back if the code is not ready. The development team monitor the state of the code by running the large unit test suite during development, and using the BuildBot continuous integration system.


= Standard library =

Python has a large standard library, commonly cited as one of Python's greatest strengths,[81] providing pre-written tools suited to many tasks. This is deliberate and has been described as a "batteries included" Python philosophy. The modules of the standard library can be augmented with custom modules written in either C or Python. Boost C++ Libraries includes a library, Boost.Python, to enable interoperability between C++ and Python. Because of the wide variety of tools provided by the standard library, combined with the ability to use a lower-level language such as C and C++, which is already capable of interfacing between other libraries, Python can be a powerful glue language between languages and tools.

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) already supported. Modules for creating graphical user interfaces, connecting to relational databases, arithmetic with arbitrary precision decimals, manipulating regular expressions, and doing unit testing are also included.

Some parts of the standard library are covered by specifications (for example, the WSGI implementation wsgiref follows PEP 333), but the majority of the modules are not. They are specified by their code, internal documentation, and test suite (if supplied). However, because most of the standard library is cross-platform Python code, there are only a few modules that must be altered or completely rewritten by alternative implementations.


For software testing, the standard library provides the unittest and doctest modules.

-----

= Influence on other languages =

Python's design and philosophy have influenced several programming languages, including:

 *Pyrex and its derivative Cython are code translators that are targeted at writing fast C extensions for the CPython interpreter. The language is mostly Python with syntax extensions for C and C++ features. Both languages produce compilable C code as output.
 *Boo uses indentation, a similar syntax, and a similar object model. However, Boo uses static typing and is closely integrated with the .NET framework.[84]
 *Cobra uses indentation and similar syntax. Cobra's "Acknowledgements" document lists Python first among the languages that influenced it. However, Cobra directly supports design-by-contract, unit tests and optional static typing.
 *\ borrowed iterators, generators, and list comprehensions from Python.
 *Go is described as incorporating the "development speed of working in a dynamic language like Python".
 *Groovy was motivated by the desire to bring the Python design philosophy to Java.
 *OCaml has an optional syntax, called twt (The Whitespace Thing), inspired by Python and Haskell.


----------

History

Python was conceived in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI in Amsterdam. Python is a successor to the ABC programming language (itself inspired by SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum is Python's principal author, and was for many years known by the humorous title Benevolent Dictator for Life (BDFL). Python evolution is now guided by an elected Steering Committee.


Programming philosophy

Python is a multi-paradigm programming language. Rather than forcing programmers to adopt a particular style of programming, it permits several styles: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming (including metaprogramming and "by magic" methods). Many other paradigms are supported using extensions, such as pyDBC and Contracts for Python which allow Design by Contract.

Rather than requiring all desired functionality to be built into the language's core, Python was designed to be highly extensible. New built-in modules can be easily written in C, C++ or Cython. Python can also be used as an extension language for existing modules and applications that need a programmable interface. This design, a small core language with a large standard library with an easily extensible interpreter, was intended by Van Rossum from the very start because of his frustrations with ABC (which espoused the opposite mindset).


Name and neologisms

An important goal of the Python developers is making Python fun to use. This is reflected in the origin of the name (based on 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.[24][25] For example, the metasyntactic variables often used in Python literature are spam and eggs, instead of the traditional foo and bar.


Usage

Python is often used as a scripting language for web applications, e.g. via mod_wsgi for the Apache web server. With Web Server Gateway Interface, a standard API has been developed to facilitate these applications. Web application frameworks like Django, Pylons, TurboGears, web2py, Flask, and Zope support developers in the design and maintenance of complex applications. Libraries like NumPy, SciPy, and Matplotlib allow Python to be used effectively in scientific computing.


Syntax and semantics

Python was intended to be a highly readable language. It is designed to have an uncluttered visual layout, frequently using English keywords where other languages use punctuation. Python requires less boilerplate than traditional manifestly typed structured languages such as C or Pascal, and has a smaller number of syntactic exceptions and special cases than either of these. For a detailed description of the differences between 2.x and 3.x versions, see History of Python.

Indentation

Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks (a feature also known as the off-side rule). An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.

Statements and control flow

Python's statements include (among others):

  • The if statement, which conditionally executes a block of code, along with else and elif (a contraction of else-if).

  • The for statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.

  • The while statement, which executes a block of code as long as its condition is true.

  • The try statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses; it also ensures that clean-up code in a finally block will always be run regardless of how the block exits.

  • The class statement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming.

  • The def statement, which defines a function or method.

  • The with statement (from Python 2.5), which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run, and releasing the lock afterward).

  • The pass statement, which serves as a NOP and can be used in place of a code block.

  • The assert statement, used during debugging to check for conditions that ought to apply.

  • The yield statement, which returns a value from a generator function. (From Python 2.5, yield is also an operator. This form is used to implement coroutines -- see below.)

Expressions

  • In Python 3, the result of the division operator / with integer operands is always a floating-point value; the operator // may be used to perform integer division (the result truncated to an integer).

    • In Python 2, the / operator on integers performs integer division. Floating-point division on integers can be achieved by converting one of the integers to a float (e.g. float(x) / y), or, since 2.2, the behavior of Python 3 can be enabled using from __future__ import division.

  • In Python, == compares by value, in contrast to Java, where it compares by reference. (Value comparisons in Java use the equals() method.) Python's is operator may be used to compare object identities (comparison by reference). Comparisons may be chained, for example a <= b <= c.

  • Python uses the words and, or, not for its boolean operators rather than the symbolic &&, ||, ! used in C.

  • Python has a type of expression known as a list comprehension. Python 2.4 extended list comprehensions into a more general expression known as a generator expression.
  • Anonymous functions are implemented using lambda expressions; however, these are limited in that the body can only be a single expression.
  • Conditional expressions in Python are written as x if c else y (different in order of operands from the ?: operator common to many other languages).

  • Python makes a distinction between lists and tuples. Lists, written as [1, 2, 3], are mutable, and cannot be used as the keys of dictionaries (dictionary keys must be immutable in Python). Tuples, written as (1, 2, 3), are immutable and thus can be used as the keys of dictionaries, provided all elements of the tuple are immutable. The parentheses around the tuple are optional in some contexts. Tuples can appear on the left side of an equal sign; hence a statement like x, y = y, x can be used to swap two variables.

  • Multiple string-formatting operations are supported. The original % operator provided behavior analogous to printf format strings in C, e.g. foo=%s bar=%d" % ("blah", 2) evaluates to foo=blah bar=2. String templates were introduced in Python 2.4. Python 3.0 brought in the string format() method, with syntax like foo={0} bar={1}".format("blah", 2). Formatted string literals ("F-strings") were introduced in 3.6, with syntax like f'foo={"blah"} bar={2}'"

  • Python has various kinds of string literals:
    • Strings are delimited by single or double quotation marks. Unlike in Unix shells, Perl and Perl-influenced languages, single quotation marks and double quotation marks function identically. Both kinds of strings use the backslash (\) as an escape character and there is no implicit string interpolation such as "$foo".

    • Triple-quoted strings, which begin and end with a series of three single or double quotation marks, may span multiple lines and function like here-documents in shells, Perl and Ruby.

Methods

Methods on objects are functions attached to the object's class; the syntax instance.method(argument) is, for normal methods and functions, syntactic sugar for Class.method(instance, argument). Python methods have an explicit self parameter to access instance data, in contrast to the implicit self in some other object-oriented programming languages (for example, Java, C++ or Ruby).

Typing

Python uses duck typing and has typed objects but untyped variable names. Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically typed, Python is strongly typed, forbidding operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.

Mathematics

Python defines the modulus operator so that the result of a % b is in the half-open interval [0,b), where b is a positive integer. When b is negative, the result lies in the interval (b,0]. However, this consequently affects how integer division is defined. To maintain the validity of the equation b * (a // b) + a % b == a, integer division is defined to round towards minus infinity. Therefore 7 // 3 is 2, but (−7) // 3 is −3. This is different from many programming languages, where the result of integer division rounds towards zero, and Python's modulus operator is consequently defined in a way that can return negative numbers.


Implementations

CPython

The mainstream Python implementation, known as CPython, is written in C meeting the C89 standard. CPython compiles Python programs into intermediate bytecode,[65] which are then executed by the virtual machine. It is distributed with a large standard library written in a mixture of C and Python. CPython ships in versions for many platforms, including Microsoft Windows and most modern Unix-like systems. CPython was intended from almost its very conception to be cross-platform; its use and development on esoteric platforms such as Amoeba, alongside more conventional ones like Unix and Mac OS, has greatly helped in this regard. Unofficial builds are also available for Android and iOS.

Alternative implementations

Jython compiles the Python program into Java byte code, which can then be executed by every Java Virtual Machine implementation. This also enables the use of Java class library functions from the Python program. IronPython follows a similar approach in order to run Python programs on the .NET Common Language Runtime. PyPy is a fast self-hosting implementation of Python, written in Python, that can output several types of bytecode, object code and intermediate languages. There also exist compilers to high-level object languages, with either unrestricted Python, a restricted subset of Python, or a language similar to Python as the source language. PyPy is of this type, compiling RPython to several languages; other examples include Pyjamas compiling to JavaScript; Shed Skin compiling to C++; and Cython and Pyrex compiling to C.

Interpretational semantics

Most Python implementations (including CPython) can function as a command-line interpreter, for which the user enters statements sequentially and receives the results immediately. In short, Python acts as a shell. While the semantics of the other modes of execution (bytecode compilation, or compilation to native code) preserve the sequential semantics, they offer a speed boost at the cost of interactivity, so they are usually only used outside of a command-line interaction (e.g., when importing a module).

Other shells add capabilities beyond those in the basic interpreter, including IDLE and IPython. While generally following the visual style of the Python shell, they implement features like auto-completion, retention of session state, and syntax highlighting.


Development

Python's development is conducted largely through the Python Enhancement Proposal (PEP) process, described in PEP 1. PEPs are standardized design documents providing general information related to Python, including proposals, descriptions, design rationales, and explanations for language features. Outstanding PEPs are reviewed and commented upon on the python-dev mailing list, which is the primary forum for discussion about the language's development and approved by the Steering Council (see PEP 13 for the governance model); specific issues are discussed in the bug tracker maintained at bugs.python.org. Development of the reference implementation takes place on the GitHub cpython repository.

CPython's public releases come in three types, distinguished by which part of the version number is incremented:

  • backward-incompatible versions, where code is expected to break and must be manually ported. The first part of the version number is incremented. These releases happen infrequently—for example, version 3.0 was released 8 years after 2.0.
  • major or 'feature' releases, which are largely compatible but introduce new features. The second part of the version number is incremented. These releases are scheduled to occur roughly every 18 months, and each major version is supported by bugfixes for several years after its release.
  • bugfix releases, which introduce no new features but fix bugs. The third and final part of the version number is incremented. These releases are made whenever a sufficient number of bugs have been fixed upstream since the last release, or roughly every 3 months. Security vulnerabilities are also patched in bugfix releases.

A number of alpha, beta, and release-candidates are also released as previews and for testing before the final release is made. Although there is a rough schedule for each release, this is often pushed back if the code is not ready. The development team monitor the state of the code by running the large unit test suite during development, and using the BuildBot continuous integration system.

Standard library

Python has a large standard library, commonly cited as one of Python's greatest strengths,[81] providing pre-written tools suited to many tasks. This is deliberate and has been described as a "batteries included" Python philosophy. The modules of the standard library can be augmented with custom modules written in either C or Python. Boost C++ Libraries includes a library, Boost.Python, to enable interoperability between C++ and Python. Because of the wide variety of tools provided by the standard library, combined with the ability to use a lower-level language such as C and C++, which is already capable of interfacing between other libraries, Python can be a powerful glue language between languages and tools.

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) already supported. Modules for creating graphical user interfaces, connecting to relational databases, arithmetic with arbitrary precision decimals, manipulating regular expressions, and doing unit testing are also included.

Some parts of the standard library are covered by specifications (for example, the WSGI implementation wsgiref follows PEP 333), but the majority of the modules are not. They are specified by their code, internal documentation, and test suite (if supplied). However, because most of the standard library is cross-platform Python code, there are only a few modules that must be altered or completely rewritten by alternative implementations.

For software testing, the standard library provides the unittest and doctest modules.


Influence on other languages

Python's design and philosophy have influenced several programming languages, including:

  • Pyrex and its derivative Cython are code translators that are targeted at writing fast C extensions for the CPython interpreter. The language is mostly Python with syntax extensions for C and C++ features. Both languages produce compilable C code as output.
  • Boo uses indentation, a similar syntax, and a similar object model. However, Boo uses static typing and is closely integrated with the .NET framework.[84]
  • Cobra uses indentation and similar syntax. Cobra's "Acknowledgements" document lists Python first among the languages that influenced it. However, Cobra directly supports design-by-contract, unit tests and optional static typing.
  • \ borrowed iterators, generators, and list comprehensions from Python.
  • Go is described as incorporating the "development speed of working in a dynamic language like Python".
  • Groovy was motivated by the desire to bring the Python design philosophy to Java.
  • OCaml has an optional syntax, called twt (The Whitespace Thing), inspired by Python and Haskell.


HowToEditPythonCode (last edited 2023-09-17 14:01:23 by MatsWichmann)

Unable to edit the page? See the FrontPage for instructions.