The
syntax
In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
of the
Python programming language
Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.
Python is dynamically-typed and garbage-collected. It supports multiple programming p ...
is the set of rules that defines how a Python program will be written and
interpreted (by both the
runtime system
In computer programming, a runtime system or runtime environment is a sub-system that exists both in the computer where a program is created, as well as in the computers where the program is intended to be run. The name comes from the compile t ...
and by human readers). The Python language has many similarities to
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
,
C, and
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
. However, there are some definite differences between the languages.
Design philosophy
Python was designed to be a highly
readable language. It has a relatively uncluttered visual layout and uses English keywords frequently where other languages
use punctuation. Python aims to be simple and consistent in the design of its syntax, encapsulated in the mantra , from the
Zen of Python
The Zen of Python is a collection of 19 "guiding principles" for writing computer programs that influence the design of the Python programming language. Software engineer Tim Peters wrote this set of principles and posted it on the Python mailing ...
.
This mantra is deliberately opposed to the
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
and
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
mantra, "
there's more than one way to do it
There's more than one way to do it (TMTOWTDI or TIMTOWTDI, pronounced ''Tim Toady'') is a Perl programming motto. The language was designed with this idea in mind, in that it “doesn't try to tell the programmer how to program.” As proponents of ...
".
Keywords
Python has 35
keywords
Keyword may refer to:
Computing
* Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic
* Index term, a term used as a keyword to documents in an information syste ...
or ''reserved words''; they cannot be used as
identifiers
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
.
*
and
*
as
*
assert
*
async
*
await
*
break
*
class
*
continue
*
def
*
del
*
elif
*
else
*
except
*
False
*
finally
*
for
*
from
*
global
*
if
*
import
*
in
*
is
*
lambda
*
None
*
nonlocal
*
not
*
or
*
pass
*
raise
*
return
*
True
*
try
*
while
*
with
*
yield
In addition, Python also has 3 ''soft keywords''. Unlike regular ''hard keywords'', soft keywords are reserved words only in limited contexts where interpreting them as keywords would make syntactic sense. These words can be used as identifiers elsewhere; You can define a function or variable named ''match'' or ''case''.
*
_
*
case
*
match
; Notes
Indentation
Python uses
whitespace
White space or whitespace may refer to:
Technology
* Whitespace characters, characters in computing that represent horizontal or vertical space
* White spaces (radio), allocated but locally unused radio frequencies
* TV White Space Database, a mec ...
to delimit
control flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an ''imper ...
blocks (following the
off-side rule
A computer programming language is said to adhere to the off-side rule of syntax if blocks in that language are expressed by their indentation. The term was coined by Peter Landin, possibly as a pun on the offside rule in association football. ...
). Python borrows this feature from its predecessor
ABC
ABC are the first three letters of the Latin script known as the alphabet.
ABC or abc may also refer to:
Arts, entertainment, and media Broadcasting
* American Broadcasting Company, a commercial U.S. TV broadcaster
** Disney–ABC Television ...
: instead of punctuation or keywords, it uses indentation to indicate the run of a
block
Block or blocked may refer to:
Arts, entertainment and media Broadcasting
* Block programming, the result of a programming strategy in broadcasting
* W242BX, a radio station licensed to Greenville, South Carolina, United States known as ''96.3 ...
.
In so-called "free-format" languages—that use the block structure derived from
ALGOL
ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
—blocks of code are set off with braces (
) or keywords. In most
coding conventions
Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language. These conventions usually cover file organization, inde ...
for these languages, programmers
conventionally indent the code within a block, to visually set it apart from the surrounding code.
A
recursive
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
function
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-oriente ...
named
foo
, which is passed a single
parameter
A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
,
x
, and if the parameter is 0 will call a different function named
bar
and otherwise will call
baz
, passing
x
, and also call itself recursively, passing
x-1
as the parameter, could be implemented like this in Python:
def foo(x):
if x 0:
bar()
else:
baz(x)
foo(x - 1)
and could be written like this in
C with
K&R indent style:
void foo(int x)
Incorrectly indented code could be misread by a human reader differently than it would be interpreted by a
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
or interpreter. For example, if the function call
foo(x - 1)
on the last line in the example above was erroneously indented to be outside the
if
/
else
block:
def foo(x):
if x 0:
bar()
else:
baz(x)
foo(x - 1)
it would cause the last line to always be executed, even when
x
is 0, resulting in an
endless recursion.
While both
space
Space is the boundless three-dimensional extent in which objects and events have relative position and direction. In classical physics, physical space is often conceived in three linear dimensions, although modern physicists usually consider ...
and
tab characters are accepted as forms of indentation and any multiple of spaces can be used, spaces are recommended and 4 spaces (as in the above examples) are recommended and are by far the most commonly used. Mixing spaces and tabs on consecutive lines is not allowed starting with Python 3 because that can create bugs which are difficult to see since many tools do not visually distinguish spaces and tabs.
Data structures
Since Python is a
dynamically-typed language, Python ''values,'' not variables, carry
type information. All
variables in Python hold
references
Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''name'' ...
to
objects
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ...
, and these references are passed to functions. Some people (including
Guido van Rossum
Guido van Rossum (; born 31 January 1956) is a Dutch programmer best known as the creator of the Python programming language, for which he was the " benevolent dictator for life" (BDFL) until he stepped down from the position on 12 July 20 ...
himself) have called this parameter-passing scheme "call by object reference". An object reference means a name, and the passed reference is an "alias", i.e. a copy of the reference to the same object, just as in C/
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
. The object's value may be changed in the called function with the "alias", for example:
>>> alist = a', 'b', 'c'>>> def my_func(al):
... al.append('x')
... print(al)
...
>>> my_func(alist)
a', 'b', 'c', 'x'>>> alist
a', 'b', 'c', 'x'
Function
my_func
changes the value of
alist
with the formal argument
al
, which is an alias of
alist
. However, any attempt to operate (assign a new object reference to) on the alias itself will have no effect on the original object.
>>> alist = a', 'b', 'c'>>> def my_func(al):
... # al.append('x')
... al = al + x'# a new list created and assigned to al means al is no more alias for alist
... print(al)
...
>>> my_func(alist)
a', 'b', 'c', 'x'>>> print(alist)
a', 'b', 'c'
In Python, non-innermost-local and not-declared-global accessible names are all aliases.
Among dynamically-typed languages, Python is moderately type-checked. Implicit
conversion
Conversion or convert may refer to:
Arts, entertainment, and media
* "Conversion" (''Doctor Who'' audio), an episode of the audio drama ''Cyberman''
* "Conversion" (''Stargate Atlantis''), an episode of the television series
* "The Conversion" ...
is defined for
numeric types (as well as
booleans), so one may validly multiply a
complex number
In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted , called the imaginary unit and satisfying the equation i^= -1; every complex number can be expressed in the form ...
by an
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
(for instance) without explicit
casting
Casting is a manufacturing process in which a liquid material is usually poured into a mold, which contains a hollow cavity of the desired shape, and then allowed to solidify. The solidified part is also known as a ''casting'', which is ejected ...
. However, there is no implicit conversion between, for example, numbers and
strings
String or strings may refer to:
*String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects
Arts, entertainment, and media Films
* ''Strings'' (1991 film), a Canadian anim ...
; 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
In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can b ...
arithmetic, it transparently supports
arbitrary-precision arithmetic
In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are li ...
,
complex number
In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted , called the imaginary unit and satisfying the equation i^= -1; every complex number can be expressed in the form ...
s, and
decimal numbers
The decimal numeral system (also called the base-ten positional numeral system and denary or decanary) is the standard system for denoting integer and non-integer numbers. It is the extension to non-integer numbers of the Hindu–Arabic numeral ...
.
Python supports a wide variety of string operations. Strings in Python are
immutable
In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created.Goetz et al. ''Java Concurrency in Practice''. Addison Wesley Professional, 2006, Section 3.4 ...
, so a string operation such as a substitution of
characters
Character or Characters may refer to:
Arts, entertainment, and media Literature
* ''Character'' (novel), a 1936 Dutch novel by Ferdinand Bordewijk
* ''Characters'' (Theophrastus), a classical Greek set of character sketches attributed to The ...
, that in other programming languages might alter the 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
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
),
tuple
In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
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 computer science, an in-place algorithm is an algorithm which transforms input using no auxiliary data structure. However, a small amount of extra storage space is allowed for auxiliary variables. The input is usually overwritten by the output ...
.
Mappings, on the other hand, are (often unordered) types implemented in the form of ''dictionaries'' that "map" a set of immutable keys to corresponding elements (much like a mathematical function). For example, one could define a dictionary having a string
"toast"
mapped to the integer
42
or vice versa. The keys in a dictionary must be of an immutable Python type, such as an integer or a string because under the hood they are implemented via a
hash function
A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called ''hash values'', ''hash codes'', ''digests'', or simply ''hashes''. The values are usually u ...
. This makes for much faster lookup times but requires keys not to change.
Dictionaries are central to the internals of Python as they reside at the core of all objects and classes: the mappings between variable names (strings) and the values which the names reference are stored as dictionaries (see
Object system
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
). Since these dictionaries are directly accessible (via an object's
__dict__
attribute),
metaprogramming
Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself ...
is a straightforward and natural process in Python.
A
set
Set, The Set, SET or SETS may refer to:
Science, technology, and mathematics Mathematics
*Set (mathematics), a collection of elements
*Category of sets, the category whose objects and morphisms are sets and total functions, respectively
Electro ...
collection type is an unindexed, unordered collection that contains no duplicates, and implements
set theoretic operations such as
union
Union commonly refers to:
* Trade union, an organization of workers
* Union (set theory), in mathematics, a fundamental operation on sets
Union may also refer to:
Arts and entertainment
Music
* Union (band), an American rock group
** ''Un ...
,
intersection
In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, their i ...
,
difference
Difference, The Difference, Differences or Differently may refer to:
Music
* ''Difference'' (album), by Dreamtale, 2005
* ''Differently'' (album), by Cassie Davis, 2009
** "Differently" (song), by Cassie Davis, 2009
* ''The Difference'' (al ...
,
symmetric difference
In mathematics, the symmetric difference of two sets, also known as the disjunctive union, is the set of elements which are in either of the sets, but not in their intersection. For example, the symmetric difference of the sets \ and \ is \.
Th ...
, and
subset
In mathematics, Set (mathematics), set ''A'' is a subset of a set ''B'' if all Element (mathematics), elements of ''A'' are also elements of ''B''; ''B'' is then a superset of ''A''. It is possible for ''A'' and ''B'' to be equal; if they are ...
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. 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
In object-oriented 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. Not all object-oriente ...
. Python also supports
multiple inheritances and
mixin
In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depend ...
s.
The language supports extensive
introspection
Introspection is the examination of one's own conscious thoughts and feelings. In psychology, the process of introspection relies on the observation of one's mental state, while in a spiritual context it may refer to the examination of one's s ...
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 a method named
__add__
on a class permits one to use the
+
operator on objects of that class.
Literals
Strings
Python has various kinds of
string literal
A string literal (computer programming), literal or anonymous string is a String (computer science), string value in the source code of a computer program. Modern Computer programming, programming languages commonly use a quoted sequence of charact ...
s.
Normal string literals
Either single or double quotes can be used to quote strings. Unlike in Unix shell languages,
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
or Perl-influenced languages such as
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
or
Groovy
''Groovy'' (or, less commonly, ''groovie'' or ''groovey'') is a slang colloquialism popular during the 1950s, '60s and '70s. It is roughly synonymous with words such as "excellent", "fashionable", or "amazing", depending on context.
History
The ...
, single quotes and double quotes function identically, i.e. there is no string interpolation of ''$foo'' expressions. However, interpolation can be done in various ways: with "f-strings" (since Python 3.6), using the
format
method or the old ''%'' string-format operator.
For instance, all of these Python statements:
print(f"I just printed pages to the printer ")
print("I just printed pages to the printer ".format(num, printer))
print("I just printed pages to the printer ".format(num, printer))
print("I just printed pages to the printer ".format(num=num, printer=printer))
print("I just printed %s pages to the printer %s" % (num, printer))
print("I just printed %(num)s pages to the printer %(printer)s" % )
are equivalent to the Perl statement:
print "I just printed $num pages to the printer $printer\n"
They build a string using the variables
num
and
printer
.
Multi-line string literals
There are also multi-line strings, which begin and end with a series of three single or double quotes and functions like
here document
In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also ...
s in
Perl
Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
and
Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
.
A simple example with
variable interpolation
In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders ...
(using the
format
method) is:
print(Dear ,
I wish you to leave Sunnydale and never return.
Not Quite Love,
.format(sender="Buffy the Vampire Slayer", recipient="Spike"))
Raw strings
Finally, all of the previously mentioned string types come in "
raw
Raw is an adjective usually describing:
* Raw materials, basic materials from which products are manufactured or made
* Raw food, uncooked food
Raw or RAW may also refer to:
Computing and electronics
* .RAW, a proprietary mass spectrometry dat ...
" varieties (denoted by placing a literal ''r'' before the opening quote), which do no backslash-interpolation and hence are very useful for
regular expression
A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or ...
s; compare
"@-quoting" in
C#. Raw strings were originally included specifically for regular expressions. Due to limitations of the tokenizer, raw strings may not have a trailing backslash. Creating a raw string holding a
Windows
Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
path ending with a backslash requires some variety of workaround (commonly, using forward slashes instead of backslashes, since Windows accepts both).
Examples include:
>>> # A Windows path, even raw strings cannot end in a backslash
>>> r"C:\Foo\Bar\Baz\"
File "", line 1
r"C:\Foo\Bar\Baz\"
^
SyntaxError: EOL while scanning string literal
>>> dos_path = r"C:\Foo\Bar\Baz\ " # avoids the error by adding
>>> dos_path.rstrip() # and removing trailing space
'C:\\Foo\\Bar\\Baz\\'
>>> quoted_dos_path = r'""'.format(dos_path)
>>> quoted_dos_path
'"C:\\Foo\\Bar\\Baz\\ "'
>>> # A regular expression matching a quoted string with possible backslash quoting
>>> re.match(r'"(( "\\\\.)*)"', quoted_dos_path).group(1).rstrip()
'C:\\Foo\\Bar\\Baz\\'
>>> code = 'foo(2, bar)'
>>> # Reverse the arguments in a two-arg function call
>>> re.sub(r'\(( ,?),( ,?)\)', r'(\2, \1)', code)
'foo(2, bar)'
>>> # Note that this won't work if either argument has parenthesis or commas in it.
Concatenation of adjacent string literals
String literals (using possibly different quote conventions) appearing contiguously and only separated by whitespace (including newlines), are allowed and are aggregated into a single long string.
Thus
title = "One Good Turn: " \
'A Natural History of the Screwdriver and the Screw'
is equivalent to
title = "One Good Turn: A Natural History of the Screwdriver and the Screw"
Unicode
Since Python 3.0, the default character set is
UTF-8
UTF-8 is a variable-width encoding, variable-length character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode'' (or ''Universal Coded Character Set'') ''Transformation Format 8-bit'' ...
both for the source code and the interpreter. In UTF-8, Unicode strings are handled like traditional byte strings. This example will work:
s = "Γειά" # Hello in Greek
print(s)
Numbers
Numeric literals in Python are of the normal sort, e.g.
0
,
-1
,
3.4
,
3.5e-8
.
Python has arbitrary-length integers and automatically increases their storage size as necessary. Prior to Python 3, there were two kinds of integral numbers: traditional fixed-size integers and "long" integers of arbitrary size. The conversion to "long" integers was performed automatically when required, and thus the programmer usually didn't have to be aware of the two integral types. In newer language versions the distinction is completely gone and all integers behave like arbitrary-length integers.
Python supports normal
floating point
In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be ...
numbers, which are created when a dot is used in a literal (e.g.
1.1
), when an integer and a floating point number are used in an expression, or as a result of some mathematical operations ("true division" via the
/
operator, or exponentiation with a negative exponent).
Python also supports
complex number
In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted , called the imaginary unit and satisfying the equation i^= -1; every complex number can be expressed in the form ...
s natively. Complex numbers are indicated with the
J
or
j
suffix, e.g.
3 + 4j
.
Lists, tuples, sets, dictionaries
Python has syntactic support for the creation of container types.
Lists (class
list
) are mutable sequences of items of arbitrary types and can be created either with the special syntax
a_list = , 2, 3, "a dog"
or using normal object creation
a_second_list = list()
a_second_list.append(4)
a_second_list.append(5)
Tuples (class
tuple
) are immutable sequences of items of arbitrary types. There is also a special syntax to create tuples
a_tuple = 1, 2, 3, "four"
a_tuple = (1, 2, 3, "four")
Although tuples are created by separating items with commas, the whole construct is usually wrapped in parentheses to increase readability. An empty tuple is denoted by
()
, while a tuple with a single value can be created with
(1,)
.
Sets (class
set
) are mutable containers of hashable items of arbitrary types, with no duplicates. The items are not ordered, but sets support iteration over the items. The syntax for set creation uses curly brackets
some_set =
Python sets are very much like
mathematical sets, and support operations like set
intersection
In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, their i ...
and
union
Union commonly refers to:
* Trade union, an organization of workers
* Union (set theory), in mathematics, a fundamental operation on sets
Union may also refer to:
Arts and entertainment
Music
* Union (band), an American rock group
** ''Un ...
. Python also features a
frozenset
class for immutable sets, see
Collection types.
Dictionaries (class
dict
) are mutable mappings tying keys and corresponding values. Python has special syntax to create dictionaries (
)
a_dictionary =
The dictionary syntax is similar to the set syntax, the difference is the presence of colons. The empty literal
results in an empty dictionary rather than an empty set, which is instead created using the non-literal constructor:
set()
.
Operators
Arithmetic
Python includes the
+
,
-
,
*
,
/
("true division"),
//
(
floor
A floor is the bottom surface of a room or vehicle. Floors vary from simple dirt in a cave to many layered surfaces made with modern technology. Floors may be stone, wood, bamboo, metal or any other material that can support the expected load ...
division),
%
(
modulus), and
**
(
exponentiation
Exponentiation is a mathematical operation, written as , involving two numbers, the '' base'' and the ''exponent'' or ''power'' , and pronounced as " (raised) to the (power of) ". When is a positive integer, exponentiation corresponds to re ...
) operators, with their usual
mathematical precedence.
In Python 3,
x / y
performs "true division", meaning that it always returns a float, even if both
x
and
y
are integers that divide evenly.
>>> 4 / 2
2.0
and
//
performs
integer division
Division is one of the four basic operations of arithmetic, the ways that numbers are combined to make new numbers. The other operations are addition, subtraction, and multiplication.
At an elementary level the division of two natural numbers ...
or ''floor division'', returning the floor of the quotient as an integer.
In Python 2 (and most other programming languages), unless explicitly requested,
x / y
performed
integer division
Division is one of the four basic operations of arithmetic, the ways that numbers are combined to make new numbers. The other operations are addition, subtraction, and multiplication.
At an elementary level the division of two natural numbers ...
, returning a float only if either input was afloat. However, because Python is a dynamically-typed language, it was not always possible to tell which operation was being performed, which often led to subtle bugs, thus prompting the introduction of the
//
operator and the change in semantics of the
/
operator in Python 3.
Comparison operators
The comparison operators, i.e.
,
!=
,
<
,
>
,
<=
,
>=
,
is
,
is not
,
in
and
not in
are used on all manner of values. Numbers, strings, sequences, and mappings can all be compared. In Python 3, disparate types (such as a
str
and an
int
) do not have a consistent relative ordering. While it was possible to compare whether some string was greater-than or less-than some integer in Python 2, this was considered a historical design quirk and was ultimately removed in Python 3.
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 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
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. The boolean values
True
and
False
were added to the language in Python 2.2.1 as constants (subclassed from
1
and
0
) and were changed to be full blown keywords in Python 3. The binary comparison operators such as
and
>
return either
True
or
False
.
The boolean operators
and
and
or
use
minimal evaluation
Minimal may refer to:
* Minimal (music genre), art music that employs limited or minimal musical materials
* "Minimal" (song), 2006 song by Pet Shop Boys
* Minimal (supermarket) or miniMAL, a former supermarket chain in Germany and Poland
* Min ...
. For example,
y 0 or x/y > 100
will never raise a divide-by-zero exception. 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
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
style. As may be expected, this makes working with lists and other collections much more straightforward.
Comprehensions
One such construction is the
list comprehension
A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
, which can be expressed with the following format:
L = apping_expression for element in source_list if filter_expression
Using list comprehension to calculate the first five powers of two:
powers_of_two = **n for n in range(1, 6)
The
Quicksort
Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
algorithm can be expressed elegantly (albeit inefficiently) 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]))
Python 2.7+ also supports set comprehensions and dictionary comprehensions.
First-class functions
In Python, functions are
first-class objects that can be created and passed around dynamically.
Python's limited support for
anonymous function
In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to ...
s is the
lambda
construct. An example is the anonymous function which squares its input, called with the argument of 5:
f = lambda x: x**2
f(5)
Lambdas are limited to containing an
expression
Expression may refer to:
Linguistics
* Expression (linguistics), a word, phrase, or sentence
* Fixed expression, a form of words with a specific meaning
* Idiom, a type of fixed expression
* Metaphorical expression, a particular word, phrase, o ...
rather than
statements
Statement or statements may refer to: Common uses
*Statement (computer science), the smallest standalone element of an imperative programming language
*Statement (logic), declarative sentence that is either true or false
*Statement, a declarative ...
, although control flow can still be implemented less elegantly within lambda by using short-circuiting, and more idiomatically with conditional expressions.
Closures
Python has had support for
lexical closures since version 2.2. Here's an example function that returns a function that
approximates the derivative of the given function:
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 computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a ''value''; or in simpler terms, a variable is a named cont ...
in Python is implicitly determined by the scope in which one assigns a value to the variable, unless scope is explicitly declared with
global
or
nonlocal
.
Note that the closure's binding of a name to some value is not mutable from within the function. Given:
>>> def foo(a, b):
... print(f'a: ')
... print(f'b: ')
... def bar(c):
... b = c
... print(f'b*: ')
... bar(a)
... print(f'b: ')
...
>>> foo(1, 2)
a: 1
b: 2
b*: 1
b: 2
and you can see that
b
, as visible from the closure's scope, retains the value it had; the changed binding of
b
inside the inner function did not propagate out. The way around this is to use a
nonlocal b
statement in
bar
. In Python 2 (which lacks
nonlocal
), the usual workaround is to use a mutable value and change that value, not the binding. E.g., a list with one element.
Generators
Introduced in Python 2.2 as an optional feature and finalized in version 2.3,
generators are Python's mechanism for
lazy evaluation
In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing).
The b ...
of a function that would otherwise return a space-prohibitive or computationally intensive list.
This is an example to lazily generating the prime numbers:
from itertools import count
def generate_primes(stop_at=None):
primes = []
for n in count(start=2):
if stop_at is not None and n > stop_at:
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
When calling this function, the returned value can be iterated over much like a list:
for i in generate_primes(100): # iterate over the primes between 0 and 100
print(i)
for i in generate_primes(): # iterate over ALL primes indefinitely
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 a 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
In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing).
The b ...
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
primes_under_million = (i for i in generate_primes() if i < 1000000)
two_thousandth_prime = islice(primes_under_million, 1999, 2000).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'' module or "roll your own" loops. In contrast, a list comprehension is functionally equivalent, but is ''greedy'' in performing all the work:
primes_under_million = for i in generate_primes(2000000) if i < 1000000two_thousandth_prime = primes_under_million999 999 or triple nine most often refers to:
* 999 (emergency telephone number), a telephone number for the emergency services in several countries
* 999 (number), an integer
* AD 999, a year
* 999 BC, a year
Books
* ''999'' (anthology) or ''999: ...
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.
Dictionary and set comprehensions
While lists and generators had comprehensions/expressions, in Python versions older than 2.7 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 2.7 and 3.0 unified all collection types by introducing dictionary and set comprehensions, similar to list comprehensions:
>>> *n for n in range(5) # regular list comprehension
, 1, 4, 9, 16>>>
>>> # set comprehension
>>>
>>> # dict comprehension
Objects
Python supports most object-oriented programming (OOP) techniques. It allows
polymorphism, not only within a
class hierarchy
A class hierarchy or inheritance tree in computer science is a classification of object types, denoting objects as the instantiations of classes (class is like a blueprint, the object is what is built from that blueprint) inter-relating the vario ...
but also by
duck typing
Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose. With nominative t ...
. 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
In object-oriented 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. Not all object-oriente ...
es, an advanced tool for enhancing classes' functionality. Naturally,
inheritance
Inheritance is the practice of receiving private property, Title (property), titles, debts, entitlements, Privilege (law), privileges, rights, and Law of obligations, obligations upon the death of an individual. The rules of inheritance differ ...
, including
multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
, is supported. Python has very limited support for private variables using
name mangling
In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
It provides a way of e ...
which is rarely used in practice as
information hiding
In computer science, information hiding is the principle of segregation of the ''design decisions'' in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decisio ...
is seen by some as
unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals. The slogan "we're all responsible users here" is used to describe this attitude.
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 computer science, referential transparency and referential opacity are properties of parts of computer programs. An expression is called ''referentially transparent'' if it can be replaced with its corresponding value (and vice-versa) withou ...
, it offers an object system but does not demand behavior. Moreover, it is always possible to redefine the class using ''properties'' (see
Properties
Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property.
Property may also refer to:
Mathematics
* Property (mathematics)
Philosophy and science
* Property (philosophy), in philosophy and ...
) 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 because the property
eggs
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 ne
method resolution orderfor multiple inheritance 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.
With statement
The
with
statement handles resources, and allows users to work with the Context Manager protocol. One function (
__enter__()
) is called when entering scope and another (
__exit__()
) when leaving. This prevents forgetting to free the resource and also handles more complicated situations such as freeing the resource when an exception occurs while it is in use. Context Managers are often used with files, database connections, test cases, etc.
Properties
Properties allow specially defined methods to be invoked on an object instance by using the same syntax as used for attribute access. An example of a class defining some properties is:
class MyClass:
def __init__(self):
self._a = None
@property
def a(self):
return self._a
@a.setter # makes the property writable
def a(self, value):
self._a = value
Descriptors
A class that defines one or more of the three special methods
__get__(self, instance, owner)
,
__set__(self, instance, value)
,
__delete__(self, instance)
can be used as a descriptor. Creating an instance of a descriptor as a class member of a second class makes the instance a property of the second class.
Class and static methods
Python allows the creation of class methods and static methods via the use of the
@classmethod
and
@staticmethod
decorators. The first argument to a class method is the class object instead of the self-reference to the instance. A static method has no special first argument. Neither the instance nor the class object is passed to a static method.
Exceptions
Python supports (and extensively uses)
exception handling
In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
as a means of testing for error conditions and other "exceptional" events in a program.
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
A mail carrier, mailman, mailwoman, postal carrier, postman, postwoman, or letter carrier (in American English), sometimes colloquially known as a postie (in Australia, Canada, New Zealand, and the United Kingdom), is an employee of a post ...
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 used as an alternative to the
if
-block, especially in
threaded situations. A commonly invoked motto is EAFP, or "It is Easier to Ask for Forgiveness than Permission," which is attributed to
Grace Hopper
Grace Brewster Hopper (; December 9, 1906 – January 1, 1992) was an American computer scientist, mathematician, and United States Navy Rear admiral (United States), rear admiral. One of the first programmers of the Harvard Mark I, Harvard Mar ...
.
The alternative, known as LBYL, or "Look Before You Leap", explicitly tests for pre-conditions.
In this first code sample, following the LBYL approach, there is an explicit check for the attribute before access:
if hasattr(spam, 'eggs'):
ham = spam.eggs
else:
handle_missing_attr()
This second sample follows the EAFP paradigm:
try:
ham = spam.eggs
except AttributeError:
handle_missing_attr()
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 Pytho
profilercan be used in specific cases to determine performance characteristics. If exceptional cases are rare, then the EAFP version will have superior
average performance
In computer science, best, worst, and average cases of a given algorithm express what the resource usage is ''at least'', ''at most'' and ''on average'', respectively. Usually the resource being considered is running time, i.e. time complexity, b ...
than the alternative. In addition, it avoids the whole class of
time-of-check-to-time-of-use
In software development, time-of-check to time-of-use (TOCTOU, TOCTTOU or TOC/TOU) is a class of software bugs caused by a race condition involving the ''checking'' of the state of a part of a system (such as a security credential) and the ''use'' ...
(TOCTTOU) vulnerabilities, other
race conditions
A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events. It becomes a bug when one or more of t ...
,
[''Python in a nutshell,'' ]Alex Martelli
Alex Martelli (born October 5, 1955) is an Italian computer engineer and Fellow of the Python Software Foundation. Since early 2005, he works for Google, Inc. in Mountain View, California, for the first few years as "Über Tech Lead," then as "Se ...
p. 134
/ref> and is compatible with duck typing
Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose. With nominative t ...
. A drawback of EAFP is that it can be used only with statements; an exception cannot be caught in a generator expression, list comprehension, or lambda function.
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. Single-line comments begin with the hash character (#
) and continue until the end of the line. Comments spanning more than one line are achieved by inserting a multi-line string (with """
or
as the delimiter on each end) that is not used in assignment or otherwise evaluated but sits in between other statements.
Commenting on a piece of code:
import sys
def getline():
return sys.stdin.readline() # Get one line and return it
Commenting a piece of code with multiple lines:
def getline():
return sys.stdin.readline() """this function
gets one line
and returns it"""
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 docblocks, docst ...
s (documentation strings), that is, strings that are located alone without assignment as the first indented line within a module, class, method or function, automatically set their contents as an attribute named __doc__
, which is intended to store a human-readable description of the object's purpose, behavior, and usage. The built-in help
function generates its output based on __doc__
attributes. 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 breaks. 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 from Python code and generate documentation. Docstring documentation can also be accessed from the interpreter with the help()
function, or from the shell with the pydoc Pydoc is the standard documentation module for the programming language Python. Similar to the functionality of Perldoc within Perl and Javadoc within Java, Pydoc allows Python programmers to access Python's documentation help files, generate text a ...
command pydoc
.
The 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.
Implementation specifics
Doct ...
standard module uses interactions copied from Python shell sessions into docstrings to create tests, whereas th
docopt
module uses them to define command-line options.
Function annotations
Function annotations (type hints) are defined in PEP 3107. They allow attaching data to the arguments and return of a function. The behavior of annotations is not defined by the language and is left to third-party frameworks. For example, a library could be written to handle static typing:
def haul(item: Haulable, *vargs: PackAnimal) -> Distance
Decorators
A decorator is any callable Python object that is used to modify a function, method, or class definition. A decorator is passed the original object being defined and returns a modified object, which is then bound to the name in the definition. Python decorators were inspired in part by Java annotation
In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can ...
s, and have a similar syntax; the decorator syntax is pure syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
, 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 is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself ...
; they enhance the action of the function or method they decorate. For example, in the sample below, viking_chorus
might cause menu_item
to be run 8 times (see Spam sketch) 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 method
A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
s or static method
A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
s, adding function attributes, tracing
Tracing may refer to:
Computer graphics
* Image tracing, digital image processing to convert raster graphics into vector graphics
* Path tracing, a method of rendering images of three-dimensional scenes such that the global illumination is faithf ...
, setting pre- and 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. Postconditions are sometimes tested using assertions wit ...
s, and synchronization
Synchronization is the coordination of events to operate a system in unison. For example, the conductor of an orchestra keeps the orchestra synchronized or ''in time''. Systems that operate with all parts in synchrony are said to be synchronou ...
, but can be used for far more, including tail recursion elimination, memoization
In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. Memoization ...
and even improving the writing of other 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))
or, using intermediate variables
def black_knight():
pass
blue_decorator = favourite_colour("Blue")
decorated_by_blue = blue_decorator(black_knight)
black_knight = invincible(decorated_by_blue)
In the example above, the favourite_colour
decorator factory
A factory, manufacturing plant or a production plant is an industrial facility, often a complex consisting of several buildings filled with machinery, where workers manufacture items or operate machines which process each item into another. T ...
takes an argument. Decorator factories must return a decorator, which is then called with the object to be decorated as its argument:
def favourite_colour(colour):
def decorator(func):
def wrapper():
print(colour)
func()
return wrapper
return decorator
This would then decorate the black_knight
function such that the color, "Blue"
, would be printed prior to the black_knight
function running. Closure ensures that the color argument is accessible to the innermost wrapper function even when it is returned and goes out of scope, which is what allows decorators to work.
Despite the name, Python decorators are not an implementation of the decorator pattern
In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often ...
. The decorator pattern is a design pattern
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
used in statically-typed
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type (computer science), type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constru ...
object-oriented programming language
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
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 language
This is a list of notable programming languages, grouped by type.
There is no overarching classification scheme for programming languages. Thus, in many cases, a language is listed under multiple headings (in this regard, see " Multiparadigm lan ...
s, such as C or Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
, 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
Easter eggs, also called Paschal eggs, are eggs that are decorated for the Christian feast of Easter, which celebrates the resurrection of Jesus. As such, Easter eggs are common during the season of Eastertide (Easter season). The oldest tr ...
that summarizes its developers' feelings on this issue. The code from __future__ import braces
raises the exception SyntaxError: not a chance
. The __future__
module is normally used to provide features from future versions of Python.
Another hidden message, the Zen of Python
The Zen of Python is a collection of 19 "guiding principles" for writing computer programs that influence the design of the Python programming language. Software engineer Tim Peters wrote this set of principles and posted it on the Python mailing ...
(a summary of Python design philosophy), is displayed when trying to import this
.
The message Hello world!
is printed when the import statement import __hello__
is used. In Python 2.7, instead of Hello world!
it prints Hello world...
.
Importing the antigravity
module opens a web browser to xkcd
''xkcd'', sometimes styled ''XKCD'', is a webcomic created in 2005 by American author Randall Munroe. The comic's tagline describes it as "a webcomic of romance, sarcasm, math, and language". Munroe states on the comic's website that the name ...
comi
353
that portrays a humorous fictional use for such a module, intended to demonstrate the ease with which Python modules enable additional functionality. In Python 3, this module also contains an implementation of the "geohash" algorithm, a reference to xkcd
''xkcd'', sometimes styled ''XKCD'', is a webcomic created in 2005 by American author Randall Munroe. The comic's tagline describes it as "a webcomic of romance, sarcasm, math, and language". Munroe states on the comic's website that the name ...
comi
426
References
External links
Python tutorial
written by the author of Python, Guido van Rossum.
Fluent Python (second edition)
How to Start Learning Python Language
{{DEFAULTSORT:Python Syntax And Semantics
Programming language syntax
Python (programming language)
Articles with example Python (programming language) code
Articles with example C code