>>>...A tool that tries to convert Python 2.x code to Python 3.x code by handling most of the incompatibilities which can be detected by parsing the source and traversing the parse tree.
2to3 is available in the standard library as lib2to3; a standalone
entry point is provided as Tools/scripts/2to3. See
2to3 - Automated Python 2 to 3 code translation.
hasattr() would be clumsy or subtly wrong (for example with
magic methods). ABCs introduce virtual
subclasses, which are classes that don’t inherit from a class but are
still recognized by isinstance() and issubclass(); see the
abc module documentation. Python comes with many built-in ABCs for
data structures (in the collections module), numbers (in the
numbers module), and streams (in the io module). You can
create your own ABCs with the abc module.A value passed to a function (or method) when calling the function. There are two types of arguments:
keyword argument: an argument preceded by an identifier (e.g.
name=) in a function call or passed as a value in a dictionary
preceded by **. For example, 3 and 5 are both keyword
arguments in the following calls to complex():
complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})
positional argument: an argument that is not a keyword argument.
Positional arguments can appear at the beginning of an argument list
and/or be passed as elements of an iterable preceded by *.
For example, 3 and 5 are both positional arguments in the
following calls:
complex(3, 5)
complex(*(3, 5))
Arguments are assigned to the named local variables in a function body. See the Calls section for the rules governing this assignment. Syntactically, any expression can be used to represent an argument; the evaluated value is assigned to the local variable.
See also the parameter glossary entry and the FAQ question on the difference between arguments and parameters.
str, bytearray or memoryview.
Bytes-like objects can be used for various operations that expect
binary data, such as compression, saving to a binary file or sending
over a socket. Some operations need the binary data to be mutable,
in which case not all bytes-like objects can apply.Python source code is compiled into bytecode, the internal representation
of a Python program in the CPython interpreter. The bytecode is also
cached in .pyc and .pyo files so that executing the same file is
faster the second time (recompilation from source to bytecode can be
avoided). This “intermediate language” is said to run on a
virtual machine that executes the machine code corresponding to
each bytecode. Do note that bytecodes are not expected to work between
different Python virtual machines, nor to be stable between Python
releases.
A list of bytecode instructions can be found in the documentation for the dis module.
object. See
new-style class. Classic classes have been removed in Python 3.int(3.15) converts the floating point number to the integer 3, but
in 3+4.5, each argument is of a different type (one int, one float),
and both must be converted to the same type before they can be added or it
will raise a TypeError. Coercion between two operands can be
performed with the coerce built-in function; thus, 3+4.5 is
equivalent to calling operator.add(*coerce(3, 4.5)) and results in
operator.add(3.0, 4.5). Without coercion, all arguments of even
compatible types would have to be normalized to the same value by the
programmer, e.g., float(3)+4.5 rather than just 3+4.5.-1), often written i in mathematics or j in
engineering. Python has built-in support for complex numbers, which are
written with this latter notation; the imaginary part is written with a
j suffix, e.g., 3+1j. To get access to complex equivalents of the
math module, use cmath. Use of complex numbers is a fairly
advanced mathematical feature. If you’re not aware of a need for them,
it’s almost certain you can safely ignore them.with
statement by defining __enter__() and __exit__() methods.
See PEP 343.A function returning another function, usually applied as a function
transformation using the @wrapper syntax. Common examples for
decorators are classmethod() and staticmethod().
The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:
def f(...):
...
f = staticmethod(f)
@staticmethod
def f(...):
...
The same concept exists for classes, but is less commonly used there. See the documentation for function definitions and class definitions for more about decorators.
Any new-style object which defines the methods __get__(),
__set__(), or __delete__(). When a class attribute is a
descriptor, its special binding behavior is triggered upon attribute
lookup. Normally, using a.b to get, set or delete an attribute looks up
the object named b in the class dictionary for a, but if b is a
descriptor, the respective descriptor method gets called. Understanding
descriptors is a key to a deep understanding of Python because they are
the basis for many features including functions, methods, properties,
class methods, static methods, and reference to super classes.
For more information about descriptors’ methods, see Implementing Descriptors.
__hash__() and __eq__() methods.
Called a hash in Perl.dict.viewkeys(), dict.viewvalues(),
and dict.viewitems() are called dictionary views. They provide a dynamic
view on the dictionary’s entries, which means that when the dictionary
changes, the view reflects these changes. To force the
dictionary view to become a full list use list(dictview). See
Dictionary view objects.__doc__ attribute
of the enclosing class, function or module. Since it is available via
introspection, it is the canonical place for documentation of the
object.type() or
isinstance(). (Note, however, that duck-typing can be complemented
with abstract base classes.) Instead, it
typically employs hasattr() tests or EAFP programming.try and except
statements. The technique contrasts with the LBYL style
common to many other languages such as C.print or if. Assignments
are also statements, not expressions.An object exposing a file-oriented API (with methods such as
read() or write()) to an underlying resource. Depending
on the way it was created, a file object can mediate access to a real
on-disk file or to another type of storage or communication device
(for example standard input/output, in-memory buffers, sockets, pipes,
etc.). File objects are also called file-like objects or
streams.
There are actually three categories of file objects: raw binary files,
buffered binary files and text files. Their interfaces are defined in the
io module. The canonical way to create a file object is by using
the open() function.
find_module(). See PEP 302 for
details.//. For example, the expression 11 // 4
evaluates to 2 in contrast to the 2.75 returned by float true
division. Note that (-11) // 4 is -3 because that is -2.75
rounded downward. See PEP 238.A pseudo-module which programmers can use to enable new language features
which are not compatible with the current interpreter. For example, the
expression 11/4 currently evaluates to 2. If the module in which
it is executed had enabled true division by executing:
from __future__ import division
the expression 11/4 would evaluate to 2.75. By importing the
__future__ module and evaluating its variables, you can see when a
new feature was first added to the language and when it will become the
default:
>>> import __future__
>>> __future__.division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
yield statements for producing a series
of values usable in a for-loop or that can be retrieved one at a time with
the next() function. Each yield temporarily suspends
processing, remembering the location execution state (including local
variables and pending try-statements). When the generator resumes, it
picks-up where it left-off (in contrast to functions which start fresh on
every invocation).An expression that returns an iterator. It looks like a normal expression
followed by a for expression defining a loop variable, range,
and an optional if expression. The combined expression
generates values for an enclosing function:
>>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81
285
The mechanism used by the CPython interpreter to assure that
only one thread executes Python bytecode at a time.
This simplifies the CPython implementation by making the object model
(including critical built-in types such as dict) implicitly
safe against concurrent access. Locking the entire interpreter
makes it easier for the interpreter to be multi-threaded, at the
expense of much of the parallelism afforded by multi-processor
machines.
However, some extension modules, either standard or third-party, are designed so as to release the GIL when doing computationally-intensive tasks such as compression or hashing. Also, the GIL is always released when doing I/O.
Past efforts to create a “free-threaded” interpreter (one which locks shared data at a much finer granularity) have not been successful because performance suffered in the common single-processor case. It is believed that overcoming this performance issue would make the implementation much more complicated and therefore costlier to maintain.
An object is hashable if it has a hash value which never changes during
its lifetime (it needs a __hash__() method), and can be compared to
other objects (it needs an __eq__() or __cmp__() method).
Hashable objects which compare equal must have the same hash value.
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable
containers (such as lists or dictionaries) are. Objects which are
instances of user-defined classes are hashable by default; they all
compare unequal (except with themselves), and their hash value is derived
from their id().
11/4 currently evaluates to 2 in contrast to the
2.75 returned by float division. Also called floor division.
When dividing two integers the outcome will always be another integer
(having the floor function applied to it). However, if one of the operands
is another numeric type (such as a float), the result will be
coerced (see coercion) to a common type. For example, an integer
divided by a float will result in a float value, possibly with a decimal
fraction. Integer division can be forced by using the // operator
instead of the / operator. See also __future__.python with no
arguments (possibly by selecting it from your computer’s main
menu). It is a very powerful way to test out new ideas or inspect
modules and packages (remember help(x)).list, str,
and tuple) and some non-sequence types like dict
and file and objects of any classes you define
with an __iter__() or __getitem__() method. Iterables can be
used in a for loop and in many other places where a sequence is
needed (zip(), map(), ...). When an iterable object is passed
as an argument to the built-in function iter(), it returns an
iterator for the object. This iterator is good for one pass over the set
of values. When using iterables, it is usually not necessary to call
iter() or deal with iterator objects yourself. The for
statement does that automatically for you, creating a temporary unnamed
variable to hold the iterator for the duration of the loop. See also
iterator, sequence, and generator.An object representing a stream of data. Repeated calls to the iterator’s
next() method return successive items in the stream. When no more
data are available a StopIteration exception is raised instead. At
this point, the iterator object is exhausted and any further calls to its
next() method just raise StopIteration again. Iterators are
required to have an __iter__() method that returns the iterator
object itself so every iterator is also iterable and may be used in most
places where other iterables are accepted. One notable exception is code
which attempts multiple iteration passes. A container object (such as a
list) produces a fresh new iterator each time you pass it to the
iter() function or use it in a for loop. Attempting this
with an iterator will just return the same exhausted iterator object used
in the previous iteration pass, making it appear like an empty container.
More information can be found in Iterator Types.
A key function or collation function is a callable that returns a value
used for sorting or ordering. For example, locale.strxfrm() is
used to produce a sort key that is aware of locale specific sort
conventions.
A number of tools in Python accept key functions to control how elements
are ordered or grouped. They include min(), max(),
sorted(), list.sort(), heapq.nsmallest(),
heapq.nlargest(), and itertools.groupby().
There are several ways to create a key function. For example. the
str.lower() method can serve as a key function for case insensitive
sorts. Alternatively, an ad-hoc key function can be built from a
lambda expression such as lambda r: (r[0], r[2]). Also,
the operator module provides three key function constructors:
attrgetter(), itemgetter(), and
methodcaller(). See the Sorting HOW TO for examples of how to create and use key functions.
lambda [arguments]: expressionLook before you leap. This coding style explicitly tests for
pre-conditions before making calls or lookups. This style contrasts with
the EAFP approach and is characterized by the presence of many
if statements.
In a multi-threaded environment, the LBYL approach can risk introducing a
race condition between “the looking” and “the leaping”. For example, the
code, if key in mapping: return mapping[key] can fail if another
thread removes key from mapping after the test, but before the lookup.
This issue can be solved with locks or by using the EAFP approach.
result = ["0x%02x" % x for x in
range(256) if x % 2 == 0] generates a list of strings containing
even hex numbers (0x..) in the range from 0 to 255. The if
clause is optional. If omitted, all elements in range(256) are
processed.load_module(). A loader is typically returned by a
finder. See PEP 302 for details.Mapping or
MutableMapping
abstract base classes. Examples
include dict, collections.defaultdict,
collections.OrderedDict and collections.Counter.The class of a class. Class definitions create a class name, a class dictionary, and a list of base classes. The metaclass is responsible for taking those three arguments and creating the class. Most object oriented programming languages provide a default implementation. What makes Python special is that it is possible to create custom metaclasses. Most users never need this tool, but when the need arises, metaclasses can provide powerful, elegant solutions. They have been used for logging attribute access, adding thread-safety, tracking object creation, implementing singletons, and many other tasks.
More information can be found in Customizing class creation.
self).
See function and nested scope.An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
See also package.
id(). See
also immutable.Any tuple-like class whose indexable elements are also accessible using
named attributes (for example, time.localtime() returns a
tuple-like object where the year is accessible either with an
index such as t[0] or with a named attribute like t.tm_year).
A named tuple can be a built-in type such as time.struct_time,
or it can be created with a regular class definition. A full featured
named tuple can also be created with the factory function
collections.namedtuple(). The latter approach automatically
provides extra features such as a self-documenting representation like
Employee(name='jones', title='programmer').
__builtin__.open() and os.open() are distinguished by their
namespaces. Namespaces also aid readability and maintainability by making
it clear which module implements a function. For instance, writing
random.seed() or itertools.izip() makes it clear that those
functions are implemented by the random and itertools
modules, respectively.Any class which inherits from object. This includes all built-in
types like list and dict. Only new-style classes can
use Python’s newer, versatile features like __slots__,
descriptors, properties, and __getattribute__().
More information can be found in New-style and classic classes.
__path__ attribute.A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept. There are four types of parameters:
positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword argument. This is the default kind of parameter, for example foo and bar in the following:
def func(foo, bar=None): ...
positional-only: specifies an argument that can be supplied only
by position. Python has no syntax for defining positional-only
parameters. However, some built-in functions have positional-only
parameters (e.g. abs()).
var-positional: specifies that an arbitrary sequence of
positional arguments can be provided (in addition to any positional
arguments already accepted by other parameters). Such a parameter can
be defined by prepending the parameter name with *, for example
args in the following:
def func(*args, **kwargs): ...
var-keyword: specifies that arbitrarily many keyword arguments
can be provided (in addition to any keyword arguments already accepted
by other parameters). Such a parameter can be defined by prepending
the parameter name with **, for example kwargs in the example
above.
Parameters can specify both optional and required arguments, as well as default values for some optional arguments.
See also the argument glossary entry, the FAQ question on the difference between arguments and parameters, and the Function definitions section.
An idea or piece of code which closely follows the most common idioms
of the Python language, rather than implementing code using concepts
common to other languages. For example, a common idiom in Python is
to loop over all elements of an iterable using a for
statement. Many other languages don’t have this type of construct, so
people unfamiliar with Python sometimes use a numerical counter instead:
for i in range(len(food)):
print food[i]
As opposed to the cleaner, Pythonic method:
for piece in food:
print piece
sys module defines a
getrefcount() function that programmers can call to return the
reference count for a particular object.__getitem__() special method and defines a
len() method that returns the length of the sequence.
Some built-in sequence types are list, str,
tuple, and unicode. Note that dict also
supports __getitem__() and __len__(), but is considered a
mapping rather than a sequence because the lookups use arbitrary
immutable keys rather than integers.[] with colons between numbers
when several are given, such as in variable_name[1:3:5]. The bracket
(subscript) notation uses slice objects internally (or in older
versions, __getslice__() and __setslice__()).if, while or for._make() or
_asdict(). Examples of struct sequences
include sys.float_info and the return value of os.stat().__class__ attribute or can be retrieved with
type(obj).'\n',
the Windows convention '\r\n', and the old Macintosh convention
'\r'. See PEP 278 and PEP 3116, as well as
str.splitlines() for an additional use.import this” at the interactive prompt.