Below is a list of the types that are built into Python. Extension modules written in C can define additional types. Future versions of Python may add types to the type hierarchy (e.g. rational or complex numbers, efficiently stored arrays of integers, etc.).
Some of the type descriptions below contain a paragraph listing
`special attributes'. These are attributes that provide access to the
implementation and are not intended for general use. Their definition
may change in the future. There are also some `generic' special
attributes, not listed with the individual objects: __methods__
is a list of the method names of a built-in object, if it has any;
__members__
is a list of the data attribute names of a built-in
object, if it has any.
None
.
It is returned from functions that don't explicitly return an object.
Python distinguishes between integers and floating point numbers:
There are two types of integers:
OverflowError
is raised.
For the purpose of shift and mask operations, integers are assumed to
have a binary, 2's complement notation using 32 or more bits, and
hiding no bits from the user (i.e., all 4294967296 different bit
patterns correspond to different values).
The rules for integer representation are intended to give the most meaningful interpretation of shift and mask operations involving negative integers and the least surprises when switching between the plain and long integer domains. For any operation except left shift, if it yields a result in the plain integer domain without causing overflow, it will yield the same result in the long integer domain or when using mixed operands.
len()
returns the number of elements
of a sequence. When this number is n, the index set contains
the numbers 0, 1, ..., n-1. Element i of sequence
a is selected by a[i]
.
Sequences also support slicing: a[i:j]
selects all elements
with index k such that i <=
k <
j. When used as an expression, a slice is a sequence of the
same type --- this implies that the index set is renumbered so that it
starts at 0 again.
Sequences are distinguished according to their mutability:
The following types are immutable sequences:
chr()
and ord()
convert between characters
and nonnegative integers representing the byte values.
Bytes with the values 0-127 represent the corresponding ASCII values.
The string data type is also used to represent arrays of bytes, e.g.
to hold data read from a file.
(On systems whose native character set is not ASCII, strings may use
EBCDIC in their internal representation, provided the functions
chr()
and ord()
implement a mapping between ASCII and
EBCDIC, and string comparison preserves the ASCII order.
Or perhaps someone can propose a better rule?)
del
(delete) statements.
There is currently a single mutable sequence type:
a[k]
selects the element indexed
by k
from the mapping a
; this can be used in
expressions and as the target of assignments or del
statements.
The built-in function len()
returns the number of elements
in a mapping.
There is currently a single mapping type:
Dictionaries are mutable; they are created by the {...}
notation (see section ).
function(argument, argument, ...)
, can be applied:
Special read-only attributes: func_code
is the code object
representing the compiled function body, and func_globals
is (a
reference to) the dictionary that holds the function's global
variables --- it implements the global name space of the module in
which the function was defined.
Special read-only attributes: im_self
is the class instance
object, im_func
is the function object.
len
and math.sin
. There
are no special attributes. The number and type of the arguments are
determined by the C function.
list.append
if
list
is a list object.
__init__
method
if it has one. Any arguments are passed on to the __init__
method --- if there is no __init__
method, the class must be called
without arguments.
import
statement (see section
func_globals
attribute of functions defined in the module).
Module attribute references are translated to lookups in this
dictionary. A module object does not contain the code object used to
initialize the module (since it isn't needed once the initialization
is done).
Attribute assignment update the module's name space dictionary.
Special read-only attributes: __dict__
yields the module's name
space as a dictionary object; __name__
yields the module's name
as a string object.
Class attribute assignments update the class's dictionary, never the dictionary of a base class.
A class can be called as a function to yield a class instance (see above).
Special read-only attributes: __dict__
yields the dictionary
containing the class's name space; __bases__
yields a tuple
(possibly empty or a singleton) containing the base classes, in the
order of their occurrence in the base class list.
Attribute assignments update the instance's dictionary.
Class instances can pretend to be numbers, sequences, or mappings if
they have methods with certain special names. These are described in
section .
Special read-only attributes: __dict__
yields the attribute
dictionary; __class__
yields the instance's class.
open()
built-in function, and also by posix.popen()
and
the makefile
method of socket objects. sys.stdin
,
sys.stdout
and sys.stderr
are file objects corresponding
to the interpreter's standard input, output and error streams.
See the Python Library Reference for methods of file objects and other
details.
Special read-only attributes: co_code
is a string representing
the sequence of instructions; co_consts
is a list of literals
used by the code; co_names
is a list of names (strings) used by
the code; co_filename
is the filename from which the code was
compiled. (To find out the line numbers, you would have to decode the
instructions; the standard library module dis
contains an
example of how to do this.)
Special read-only attributes: f_back
is to the previous
stack frame (towards the caller), or None
if this is the bottom
stack frame; f_code
is the code object being executed in this
frame; f_globals
is the dictionary used to look up global
variables; f_locals
is used for local variables;
f_lineno
gives the line number and f_lasti
gives the
precise instruction (this is an index into the instruction string of
the code object).
sys.exc_traceback
. When the
program contains no suitable handler, the stack trace is written
(nicely formatted) to the standard error stream; if the interpreter is
interactive, it is also made available to the user as
sys.last_traceback
.
Special read-only attributes: tb_next
is the next level in the
stack trace (towards the frame where the exception occurred), or
None
if there is no next level; tb_frame
points to the
execution frame of the current level; tb_lineno
gives the line
number where the exception occurred; tb_lasti
indicates the
precise instruction. The line number and last instruction in the
traceback may differ from the line number of its frame object if the
exception occurred in a try
statement with no matching
except
clause or with a finally
clause.