3. Modelo de datos

3.1. Objetos, valores y tipos

Objects son la abstracción de Python para los datos. Todos los datos en un programa Python están representados por objetos o por relaciones entre objetos. (En cierto sentido y de conformidad con el modelo de Von Neumann de una «programa almacenado de computadora», el código también está representado por objetos.)

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The is operator compares the identity of two objects; the id() function returns an integer representing its identity.

Detalles de implementación de CPython: Para CPython, id(x) es la dirección de memoria donde se almacena x.

El tipo de un objeto determina las operaciones que admite el objeto (por ejemplo, «¿tiene una longitud?») y también define los posibles valores para los objetos de ese tipo. La función type() retorna el tipo de un objeto (que es un objeto en sí mismo). Al igual que su identidad, también el type de un objeto es inmutable. [1]

El valor de algunos objetos puede cambiar. Se dice que los objetos cuyo valor puede cambiar son mutables; Los objetos cuyo valor no se puede modificar una vez que se crean se denominan inmutables. (El valor de un objeto contenedor inmutable que contiene una referencia a un objeto mutable puede cambiar cuando se cambia el valor de este último; sin embargo, el contenedor todavía se considera inmutable, porque la colección de objetos que contiene no se puede cambiar. Por lo tanto, la inmutabilidad no es estrictamente lo mismo que tener un valor inmutable, es más sutil). La mutabilidad de un objeto está determinada por su tipo; por ejemplo, los números, las cadenas de caracteres y las tuplas son inmutables, mientras que los diccionarios y las listas son mutables.

Los objetos nunca se destruyen explícitamente; sin embargo, cuando se vuelven inalcanzables, se pueden recolectar basura. Se permite a una implementación posponer la recolección de basura u omitirla por completo; es una cuestión de calidad de la implementación cómo se implementa la recolección de basura, siempre que no se recolecten objetos que todavía sean accesibles.

Detalles de implementación de CPython: CPython actualmente utiliza un esquema de conteo de referencias con detección retardada (opcional) de basura enlazada cíclicamente, que recolecta la mayoría de los objetos tan pronto como se vuelven inalcanzables, pero no se garantiza que recolecte basura que contenga referencias circulares. Vea la documentación del módulo gc para información sobre el control de la recolección de basura cíclica. Otras implementaciones actúan de manera diferente y CPython puede cambiar. No dependa de la finalización inmediata de los objetos cuando se vuelvan inalcanzables (por lo que siempre debe cerrar los archivos explícitamente).

Note that the use of the implementation’s tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a tryexcept statement may keep objects alive.

Some objects contain references to «external» resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a close() method. Programs are strongly recommended to explicitly close such objects. The tryfinally statement and the with statement provide convenient ways to do this.

Algunos objetos contienen referencias a otros objetos; estos se llaman contenedores. Ejemplos de contenedores son tuplas, listas y diccionarios. Las referencias son parte del valor de un contenedor. En la mayoría de los casos, cuando hablamos del valor de un contenedor, implicamos los valores, no las identidades de los objetos contenidos; sin embargo, cuando hablamos de la mutabilidad de un contenedor, solo se implican las identidades de los objetos contenidos inmediatamente. Entonces, si un contenedor inmutable (como una tupla) contiene una referencia a un objeto mutable, su valor cambia si se cambia ese objeto mutable.

Los tipos afectan a casi todos los aspectos del comportamiento del objeto. Incluso la importancia de la identidad del objeto se ve afectada en cierto sentido: para los tipos inmutables, las operaciones que calculan nuevos valores en realidad pueden retornar una referencia a cualquier objeto existente con el mismo tipo y valor, mientras que para los objetos mutables esto no está permitido. Por ejemplo, al hacer a = 1; b = 1, a y b puede o no referirse al mismo objeto con el valor 1, dependiendo de la implementación, pero al hacer c = []; d = [], c y d se garantiza que se refieren a dos listas vacías diferentes, únicas y recién creadas. (Tenga en cuenta que c = d = [] asigna el mismo objeto a ambos c y d.)

3.2. Jerarquía de tipos estándar

A continuación se muestra una lista de los tipos integrados en Python. Los módulos de extensión (escritos en C, Java u otros lenguajes, dependiendo de la implementación) pueden definir tipos adicionales. Las versiones futuras de Python pueden agregar tipos a la jerarquía de tipos (por ejemplo, números racionales, matrices de enteros almacenados de manera eficiente, etc.), aunque tales adiciones a menudo se proporcionarán a través de la biblioteca estándar.

Algunas de las descripciones de tipos a continuación contienen un párrafo que enumera “atributos especiales”. Estos son atributos que proporcionan acceso a la implementación y no están destinados para uso general. Su definición puede cambiar en el futuro.

3.2.1. None

Este tipo tiene un solo valor. Hay un solo objeto con este valor. Se accede a este objeto a través del nombre incorporado None. Se utiliza para indicar la ausencia de un valor en muchas situaciones, por ejemplo, se retorna desde funciones que no retornan nada explícitamente. Su valor de verdad es falso.

3.2.2. NotImplemented

This type has a single value. There is a single object with this value. This object is accessed through the built-in name NotImplemented. Numeric methods and rich comparison methods should return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) It should not be evaluated in a boolean context.

Vea Implementar operaciones aritméticas para más detalles.

Distinto en la versión 3.9: Evaluating NotImplemented in a boolean context is deprecated. While it currently evaluates as true, it will emit a DeprecationWarning. It will raise a TypeError in a future version of Python.

3.2.3. Elipsis

Este tipo tiene un solo valor. Hay un solo objeto con este valor. Se accede a este objeto a través del literal ... o el nombre incorporado Ellipsis. Su valor de verdad es verdadero.

3.2.4. numbers.Number

Estos son creados por literales numéricos y retornados como resultados por operadores aritméticos y funciones aritméticas integradas. Los objetos numéricos son inmutables; una vez creado su valor nunca cambia. Los números de Python están, por supuesto, fuertemente relacionados con los números matemáticos, pero están sujetos a las limitaciones de la representación numérica en las computadoras.

The string representations of the numeric classes, computed by __repr__() and __str__(), have the following properties:

  • Son literales numéricos válidos que, cuando se pasan a su constructor de clase, producen un objeto que tiene el valor del numérico original.

  • La representación está en base 10, cuando sea posible.

  • Los ceros iniciales, posiblemente excepto un solo cero antes de un punto decimal, no se muestran.

  • Los ceros finales, posiblemente excepto un solo cero después de un punto decimal, no se muestran.

  • Solo se muestra un signo cuando el número es negativo.

Python distingue entre números enteros, números de coma flotante y números complejos:

3.2.4.1. numbers.Integral

Estos representan elementos del conjunto matemático de números enteros (positivo y negativo).

Nota

Las reglas para la representación de enteros están destinadas a dar la interpretación más significativa de las operaciones de cambio y máscara que involucran enteros negativos.

Hay dos tipos de números enteros:

Enteros (int)

Estos representan números en un rango ilimitado, sujetos solo a la memoria (virtual) disponible. Para las operaciones de desplazamiento y máscara, se asume una representación binaria, y los números negativos se representan en una variante del complemento de 2 que da la ilusión de una cadena de caracteres infinita de bits con signo que se extiende hacia la izquierda.

Booleanos (bool)

Estos representan los valores de verdad Falso y Verdadero. Los dos objetos que representan los valores False y True son los únicos objetos booleanos. El tipo booleano es un subtipo del tipo entero y los valores booleanos se comportan como los valores 0 y 1 respectivamente, en casi todos los contextos, con la excepción de que cuando se convierten en una cadena de caracteres, las cadenas de caracteres "False" o "True" son retornadas respectivamente.

3.2.4.2. numbers.Real (float)

Estos representan números de punto flotante de precisión doble a nivel de máquina. Está a merced de la arquitectura de la máquina subyacente (y la implementación de C o Java) para el rango aceptado y el manejo del desbordamiento. Python no admite números de coma flotante de precisión simple; el ahorro en el uso del procesador y la memoria, que generalmente son la razón para usarlos, se ven reducidos por la sobrecarga del uso de objetos en Python, por lo que no hay razón para complicar el lenguaje con dos tipos de números de coma flotante.

3.2.4.3. numbers.Complex (complex)

Estos representan números complejos como un par de números de coma flotante de precisión doble a nivel de máquina. Se aplican las mismas advertencias que para los números de coma flotante. Las partes reales e imaginarias de un número complejo z se pueden obtener a través de los atributos de solo lectura z.real y z.imag.

3.2.5. Secuencias

These represent finite ordered sets indexed by non-negative numbers. The built-in function len() returns the number of items of a sequence. When the length of a sequence is n, the index set contains the numbers 0, 1, …, n-1. Item i of sequence a is selected by a[i]. Some sequences, including built-in sequences, interpret negative subscripts by adding the sequence length. For example, a[-2] equals a[n-2], the second to last item of sequence a with length n.

Sequences also support slicing: a[i:j] selects all items with index k such that i <= k < j. When used as an expression, a slice is a sequence of the same type. The comment above about negative indexes also applies to negative slice positions.

Algunas secuencias también admiten «segmentación extendida» con un tercer parámetro «paso» : a[i:j:k] selecciona todos los elementos de a con índice x donde x = i + n*k, n >= 0 y i <= x < j.

Las secuencias se distinguen según su mutabilidad:

3.2.5.1. Secuencias inmutables

Un objeto de un tipo de secuencia inmutable no puede cambiar una vez que se crea. (Si el objeto contiene referencias a otros objetos, estos otros objetos pueden ser mutables y pueden cambiarse; sin embargo, la colección de objetos a los que hace referencia directamente un objeto inmutable no puede cambiar).

Los siguientes tipos son secuencias inmutables:

Cadenas de caracteres

A string is a sequence of values that represent Unicode code points. All the code points in the range U+0000 - U+10FFFF can be represented in a string. Python doesn’t have a char type; instead, every code point in the string is represented as a string object with length 1. The built-in function ord() converts a code point from its string form to an integer in the range 0 - 10FFFF; chr() converts an integer in the range 0 - 10FFFF to the corresponding length 1 string object. str.encode() can be used to convert a str to bytes using the given text encoding, and bytes.decode() can be used to achieve the opposite.

Tuplas

Los elementos de una tupla son objetos arbitrarios de Python. Las tuplas de dos o más elementos están formadas por listas de expresiones separadas por comas. Se puede formar una tupla de un elemento (un “singleton”) al colocar una coma en una expresión (una expresión en sí misma no crea una tupla, ya que los paréntesis deben ser utilizables para agrupar expresiones). Una tupla vacía puede estar formada por un par de paréntesis vacío.

Bytes

Un objeto de bytes es una colección inmutable. Los elementos son bytes de 8 bits, representados por enteros en el rango 0 <= x <256. Literales de bytes (como b'abc') y el constructor incorporado bytes() se puede utilizar para crear objetos de bytes. Además, los objetos de bytes se pueden decodificar en cadenas de caracteres a través del método decode().

3.2.5.2. Secuencias mutables

Las secuencias mutables se pueden cambiar después de su creación. Las anotaciones de suscripción y segmentación se pueden utilizar como el objetivo de asignaciones y declaraciones del (eliminar).

Nota

The collections and array module provide additional examples of mutable sequence types.

Actualmente hay dos tipos intrínsecos de secuencias mutable:

Listas

Los elementos de una lista son objetos de Python arbitrarios. Las listas se forman colocando una lista de expresiones separadas por comas entre corchetes. (Tome en cuenta que no hay casos especiales necesarios para formar listas de longitud 0 o 1.)

Colecciones de bytes

Un objeto bytearray es una colección mutable. Son creados por el constructor incorporado bytearray(). Además de ser mutables (y, por lo tanto, inquebrantable), las colecciones de bytes proporcionan la misma interfaz y funcionalidad que los objetos inmutables bytes.

3.2.6. Tipos de conjuntos

Estos representan conjuntos finitos no ordenados de objetos únicos e inmutables. Como tal, no pueden ser indexados por ningún subscript. Sin embargo, pueden repetirse y la función incorporada len() retorna el número de elementos en un conjunto. Los usos comunes de los conjuntos son pruebas rápidas de membresía, eliminación de duplicados de una secuencia y cálculo de operaciones matemáticas como intersección, unión, diferencia y diferencia simétrica.

Para elementos del conjunto, se aplican las mismas reglas de inmutabilidad que para las claves de diccionario. Tenga en cuenta que los tipos numéricos obedecen las reglas normales para la comparación numérica: si dos números se comparan igual (por ejemplo, 1 y 1.0), solo uno de ellos puede estar contenido en un conjunto.

Actualmente hay dos tipos de conjuntos intrínsecos:

Conjuntos

Estos representan un conjunto mutable. Son creados por el constructor incorporado set() y puede ser modificado posteriormente por varios métodos, como add().

Conjuntos congelados

Estos representan un conjunto inmutable. Son creados por el constructor incorporado frozenset(). Como un conjunto congelado es inmutable y hashable, se puede usar nuevamente como un elemento de otro conjunto o como una clave de un diccionario.

3.2.7. Mapeos

Estos representan conjuntos finitos de objetos indexados por conjuntos de índices arbitrarios. La notación de subíndice a[k] selecciona el elemento indexado por k del mapeo a; esto se puede usar en expresiones y como el objetivo de asignaciones o declaraciones del. La función incorporada len() retorna el número de elementos en un mapeo.

Actualmente hay un único tipo de mapeo intrínseco:

3.2.7.1. Diccionarios

Estos representan conjuntos finitos de objetos indexados por valores casi arbitrarios. Los únicos tipos de valores no aceptables como claves son valores que contienen listas o diccionarios u otros tipos mutables que se comparan por valor en lugar de por identidad de objeto, la razón es que la implementación eficiente de los diccionarios requiere que el valor hash de una clave permanezca constante. Los tipos numéricos utilizados para las claves obedecen las reglas normales para la comparación numérica: si dos números se comparan igual (por ejemplo, 1 y 1.0) entonces se pueden usar indistintamente para indexar la misma entrada del diccionario.

Los diccionarios conservan el orden de inserción, lo que significa que las claves se mantendrán en el mismo orden en que se agregaron secuencialmente sobre el diccionario. Reemplazar una clave existente no cambia el orden, sin embargo, eliminar una clave y volver a insertarla la agregará al final en lugar de mantener su lugar anterior.

Los diccionarios son mutables; pueden ser creados por la notación {...} (vea la sección Despliegues de diccionario).

Los módulos de extensión dbm.ndbm y dbm.gnu proporcionan ejemplos adicionales de tipos de mapeo, al igual que el módulo collections.

Distinto en la versión 3.7: Los diccionarios no conservaban el orden de inserción en las versiones de Python anteriores a 3.6. En CPython 3.6, el orden de inserción se conserva, pero se consideró un detalle de implementación en ese momento en lugar de una garantía de idioma.

3.2.8. Tipos invocables

Estos son los tipos a los que la operación de llamada de función (vea la sección Invocaciones) puede ser aplicado:

3.2.8.1. Funciones definidas por el usuario

Un objeto función definido por el usuario, es creado por un definición de función (vea la sección Definiciones de funciones). Debe llamarse con una lista de argumentos que contenga el mismo número de elementos que la lista de parámetros formales de la función.

3.2.8.1.1. Special read-only attributes

Atributo

Significado

function.__globals__

A reference to the dictionary that holds the function’s global variables – the global namespace of the module in which the function was defined.

function.__closure__

None or a tuple of cells that contain bindings for the function’s free variables.

Un objeto de celda tiene el atributo cell_contents. Esto se puede usar para obtener el valor de la celda, así como para establecer el valor.

3.2.8.1.2. Special writable attributes

Most of these attributes check the type of the assigned value:

Atributo

Significado

function.__doc__

The function’s documentation string, or None if unavailable. Not inherited by subclasses.

function.__name__

The function’s name. See also: __name__ attributes.

function.__qualname__

The function’s qualified name. See also: __qualname__ attributes.

Nuevo en la versión 3.3.

function.__module__

El nombre del módulo en el que se definió la función, o None si no está disponible.

function.__defaults__

A tuple containing default parameter values for those parameters that have defaults, or None if no parameters have a default value.

function.__code__

The code object representing the compiled function body.

function.__dict__

The namespace supporting arbitrary function attributes. See also: __dict__ attributes.

function.__annotations__

A dictionary containing annotations of parameters. The keys of the dictionary are the parameter names, and 'return' for the return annotation, if provided. See also: Prácticas recomendadas para las anotaciones.

function.__kwdefaults__

A dictionary containing defaults for keyword-only parameters.

Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes.

Detalles de implementación de CPython: CPython’s current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.

Additional information about a function’s definition can be retrieved from its code object (accessible via the __code__ attribute).

3.2.8.2. Métodos de instancia

Un objeto de método de instancia combina una clase, una instancia de clase y cualquier objeto invocable (normalmente una función definida por el usuario).

Special read-only attributes:

method.__self__

Refers to the class instance object to which the method is bound

method.__func__

Refers to the original function object

method.__doc__

The method’s documentation (same as method.__func__.__doc__). A string if the original function had a docstring, else None.

method.__name__

The name of the method (same as method.__func__.__name__)

method.__module__

The name of the module the method was defined in, or None if unavailable.

Methods also support accessing (but not setting) the arbitrary function attributes on the underlying function object.

User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a classmethod object.

When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its __self__ attribute is the instance, and the method object is said to be bound. The new method’s __func__ attribute is the original function object.

When an instance method object is created by retrieving a classmethod object from a class or instance, its __self__ attribute is the class itself, and its __func__ attribute is the function object underlying the class method.

When an instance method object is called, the underlying function (__func__) is called, inserting the class instance (__self__) in front of the argument list. For instance, when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1).

When an instance method object is derived from a classmethod object, the «class instance» stored in __self__ will actually be the class itself, so that calling either x.f(1) or C.f(1) is equivalent to calling f(C,1) where f is the underlying function.

Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance. In some cases, a fruitful optimization is to assign the attribute to a local variable and call that local variable. Also notice that this transformation only happens for user-defined functions; other callable objects (and all non-callable objects) are retrieved without transformation. It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this only happens when the function is an attribute of the class.

3.2.8.3. Funciones generadoras

A function or method which uses the yield statement (see section La declaración yield) is called a generator function. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s iterator.__next__() method will cause the function to execute until it provides a value using the yield statement. When the function executes a return statement or falls off the end, a StopIteration exception is raised and the iterator will have reached the end of the set of values to be returned.

3.2.8.4. Funciones de corrutina

Una función o método que es definido utilizando async def se llama coroutine function. Dicha función, cuando es invocada, retorna un objeto coroutine. Éste puede contener expresiones await, así como declaraciones async with y async for. Ver también la sección Objetos de corrutina.

3.2.8.5. Funciones generadoras asincrónicas

A function or method which is defined using async def and which uses the yield statement is called a asynchronous generator function. Such a function, when called, returns an asynchronous iterator object which can be used in an async for statement to execute the body of the function.

Calling the asynchronous iterator’s aiterator.__anext__ method will return an awaitable which when awaited will execute until it provides a value using the yield expression. When the function executes an empty return statement or falls off the end, a StopAsyncIteration exception is raised and the asynchronous iterator will have reached the end of the set of values to be yielded.

3.2.8.6. Funciones incorporadas

A built-in function object is a wrapper around a C function. Examples of built-in functions are len() and math.sin() (math is a standard built-in module). The number and type of the arguments are determined by the C function. Special read-only attributes:

  • __doc__ is the function’s documentation string, or None if unavailable. See function.__doc__.

  • __name__ is the function’s name. See function.__name__.

  • __self__ is set to None (but see the next item).

  • __module__ is the name of the module the function was defined in or None if unavailable. See function.__module__.

3.2.8.7. Métodos incorporados

This is really a different disguise of a built-in function, this time containing an object passed to the C function as an implicit extra argument. An example of a built-in method is alist.append(), assuming alist is a list object. In this case, the special read-only attribute __self__ is set to the object denoted by alist. (The attribute has the same semantics as it does with other instance methods.)

3.2.8.8. Clases

Classes are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override __new__(). The arguments of the call are passed to __new__() and, in the typical case, to __init__() to initialize the new instance.

3.2.8.9. Instancias de clases

Instances of arbitrary classes can be made callable by defining a __call__() method in their class.

3.2.9. Módulos

Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the import statement, or by calling functions such as importlib.import_module() and built-in __import__(). A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the __globals__ attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., m.x is equivalent to m.__dict__["x"]. A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done).

La asignación de atributos actualiza el diccionario de espacio de nombres del módulo, p. ej., m.x = 1 es equivalente a m.__dict__[“x”] = 1.

Atributos predefinidos (escribibles):

__name__

El nombre del módulo.

__doc__

El texto de documentación del módulo, o None si no está disponible.

__file__

El nombre de ruta del archivo desde el que se cargó el módulo, si se cargó desde un archivo. El atributo __file__ puede faltar para ciertos tipos de módulos, como los módulos C que están vinculados estáticamente al intérprete. Para los módulos de extensión cargados dinámicamente desde una biblioteca compartida, es el nombre de ruta del archivo de la biblioteca compartida.

__annotations__

Un diccionario que contiene el variable annotations recopilados durante la ejecución del cuerpo del módulo. Para buenas prácticas sobre trabajar con __annotations__, por favor ve Prácticas recomendadas para las anotaciones.

El atributo especial de solo lectura __dict__ es el espacio de nombres del módulo como un objeto de diccionario.

Detalles de implementación de CPython: Debido a la manera en la que CPython limpia los diccionarios de módulo, el diccionario de módulo será limpiado cuando el módulo se encuentra fuera de alcance, incluso si el diccionario aún tiene referencias existentes. Para evitar esto, copie el diccionario o mantenga el módulo cerca mientras usa el diccionario directamente.

3.2.10. Clases personalizadas

Los tipos de clases personalizadas son normalmente creadas por definiciones de clases (ver sección Definiciones de clase). Una clase tiene implementado un espacio de nombres por un objeto de diccionario. Las referencias de atributos de clase son traducidas a búsquedas en este diccionario, p. ej., C.x es traducido a C.__dict__[“x”] (aunque hay una serie de enlaces que permiten la ubicación de atributos por otros medios). Cuando el nombre de atributo no es encontrado ahí, la búsqueda de atributo continúa en las clases base. Esta búsqueda de las clases base utiliza la orden de resolución de métodos C3 que se comporta correctamente aún en la presencia de estructuras de herencia ‘diamante’ donde existen múltiples rutas de herencia que llevan a un ancestro común. Detalles adicionales en el MRO C3 utilizados por Python pueden ser encontrados en la documentación correspondiente a la versión 2.3 en https://www.python.org/download/releases/2.3/mro/.

When a class attribute reference (for class C, say) would yield a class method object, it is transformed into an instance method object whose __self__ attribute is C. When it would yield a staticmethod object, it is transformed into the object wrapped by the static method object. See section Implementando descriptores for another way in which attributes retrieved from a class may differ from those actually contained in its __dict__.

Las asignaciones de atributos de clase actualizan el diccionario de la clase, nunca el diccionario de la clase base.

Un objeto de clase puede ser invocado (ver arriba) para producir una instancia de clase (ver a continuación).

Atributos especiales:

__name__

El nombre de la clase.

__module__

El nombre del módulo en el que se definió la clase.

__dict__

El diccionario conteniendo el espacio de nombres de la clase.

__bases__

Una tupla conteniendo las clases de base, en orden de ocurrencia en la lista de clase base.

__doc__

El texto de documentación de la clase, o None si no está disponible.

__annotations__

Un diccionario conteniendo el variable annotations recopilados durante la ejecución del cuerpo de la clase. Para buenas prácticas sobre trabajar con __annotations__, por favor ve Prácticas recomendadas para las anotaciones.

3.2.11. Instancias de clase

A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. If a class attribute is found that is a user-defined function object, it is transformed into an instance method object whose __self__ attribute is the instance. Static method and class method objects are also transformed; see above under «Classes». See section Implementando descriptores for another way in which attributes of a class retrieved via its instances may differ from the objects actually stored in the class’s __dict__. If no class attribute is found, and the object’s class has a __getattr__() method, that is called to satisfy the lookup.

Attribute assignments and deletions update the instance’s dictionary, never a class’s dictionary. If the class has a __setattr__() or __delattr__() method, this is called instead of updating the instance dictionary directly.

Instancias de clases pueden pretender ser números, secuencias o mapeos si tienen métodos con ciertos nombres especiales. Ver sección Nombres especiales de método.

Atributos especiales: __dict__ es el diccionario de atributos; __class__ es la clase de la instancia.

3.2.12. Objetos E/S (también conocidos como objetos de archivo)

Un file object representa un archivo abierto. Diversos accesos directos se encuentran disponibles para crear objetos de archivo: la función incorporada open(), así como os.popen(), os.fdopen(), y el método de objetos socket makefile() (y quizás por otras funciones y métodos proporcionados por módulos de extensión).

Los objetos sys.stdin, sys.stdout y sys.stderr son iniciados a objetos de archivos correspondientes a la entrada y salida estándar del intérprete, así como flujos de error; todos ellos están abiertos en el modo de texto y por lo tanto siguen la interface definida por la clase abstracta io.TextIOBase.

3.2.13. Tipos internos

Algunos tipos utilizados internamente por el intérprete son expuestos al usuario. Sus definiciones pueden cambiar en futuras versiones del intérprete, pero son mencionadas aquí para complementar.

3.2.13.1. Objetos de código

Los objetos de código representan código de Python ejecutable compilado por bytes, o bytecode. La diferencia entre un objeto de código y un objeto de función es que el objeto de función contiene una referencia explícita a los globales de la función (el módulo en el que fue definido), mientras el objeto de código no contiene contexto; de igual manera los valores por defecto de los argumentos son almacenados en el objeto de función, no en el objeto de código (porque representan valores calculados en tiempo de ejecución). A diferencia de objetos de función, los objetos de código son inmutables y no contienen referencias (directas o indirectas) a objetos mutables.

3.2.13.1.1. Special read-only attributes
codeobject.co_name

The function name

codeobject.co_qualname

The fully qualified function name

Nuevo en la versión 3.11.

codeobject.co_argcount

The total number of positional parameters (including positional-only parameters and parameters with default values) that the function has

codeobject.co_posonlyargcount

The number of positional-only parameters (including arguments with default values) that the function has

codeobject.co_kwonlyargcount

The number of keyword-only parameters (including arguments with default values) that the function has

codeobject.co_nlocals

The number of local variables used by the function (including parameters)

codeobject.co_varnames

A tuple containing the names of the local variables in the function (starting with the parameter names)

codeobject.co_cellvars

A tuple containing the names of local variables that are referenced by nested functions inside the function

codeobject.co_freevars

A tuple containing the names of free variables in the function

codeobject.co_code

A string representing the sequence of bytecode instructions in the function

codeobject.co_consts

A tuple containing the literals used by the bytecode in the function

codeobject.co_names

A tuple containing the names used by the bytecode in the function

codeobject.co_filename

The name of the file from which the code was compiled

codeobject.co_firstlineno

The line number of the first line of the function

codeobject.co_lnotab

A string encoding the mapping from bytecode offsets to line numbers. For details, see the source code of the interpreter.

codeobject.co_stacksize

The required stack size of the code object

codeobject.co_flags

An integer encoding a number of flags for the interpreter.

The following flag bits are defined for co_flags: bit 0x04 is set if the function uses the *arguments syntax to accept an arbitrary number of positional arguments; bit 0x08 is set if the function uses the **keywords syntax to accept arbitrary keyword arguments; bit 0x20 is set if the function is a generator. See Objetos de código Bit Flags for details on the semantics of each flags that might be present.

Future feature declarations (from __future__ import division) also use bits in co_flags to indicate whether a code object was compiled with a particular feature enabled: bit 0x2000 is set if the function was compiled with future division enabled; bits 0x10 and 0x1000 were used in earlier versions of Python.

Other bits in co_flags are reserved for internal use.

If a code object represents a function, the first item in co_consts is the documentation string of the function, or None if undefined.

3.2.13.1.2. Methods on code objects
codeobject.co_positions()

Returns an iterable over the source code positions of each bytecode instruction in the code object.

The iterator returns tuples containing the (start_line, end_line, start_column, end_column). The i-th tuple corresponds to the position of the source code that compiled to the i-th instruction. Column information is 0-indexed utf-8 byte offsets on the given source line.

This positional information can be missing. A non-exhaustive lists of cases where this may happen:

  • Running the interpreter with -X no_debug_ranges.

  • Loading a pyc file compiled while using -X no_debug_ranges.

  • Position tuples corresponding to artificial instructions.

  • Line and column numbers that can’t be represented due to implementation specific limitations.

When this occurs, some or all of the tuple elements can be None.

Nuevo en la versión 3.11.

Nota

This feature requires storing column positions in code objects which may result in a small increase of disk usage of compiled Python files or interpreter memory usage. To avoid storing the extra information and/or deactivate printing the extra traceback information, the -X no_debug_ranges command line flag or the PYTHONNODEBUGRANGES environment variable can be used.

codeobject.co_lines()

Returns an iterator that yields information about successive ranges of bytecodes. Each item yielded is a (start, end, lineno) tuple:

  • start (an int) represents the offset (inclusive) of the start of the bytecode range

  • end (an int) represents the offset (exclusive) of the end of the bytecode range

  • lineno is an int representing the line number of the bytecode range, or None if the bytecodes in the given range have no line number

The items yielded will have the following properties:

  • The first range yielded will have a start of 0.

  • The (start, end) ranges will be non-decreasing and consecutive. That is, for any pair of tuples, the start of the second will be equal to the end of the first.

  • No range will be backwards: end >= start for all triples.

  • The last tuple yielded will have end equal to the size of the bytecode.

Zero-width ranges, where start == end, are allowed. Zero-width ranges are used for lines that are present in the source code, but have been eliminated by the bytecode compiler.

Nuevo en la versión 3.10.

Ver también

PEP 626 - Precise line numbers for debugging and other tools.

The PEP that introduced the co_lines() method.

codeobject.replace(**kwargs)

Return a copy of the code object with new values for the specified fields.

Nuevo en la versión 3.8.

3.2.13.2. Objetos de marco

Frame objects represent execution frames. They may occur in traceback objects, and are also passed to registered trace functions.

3.2.13.2.1. Special read-only attributes
frame.f_back

Points to the previous stack frame (towards the caller), or None if this is the bottom stack frame

frame.f_code

The code object being executed in this frame. Accessing this attribute raises an auditing event object.__getattr__ with arguments obj and "f_code".

frame.f_locals

The dictionary used by the frame to look up local variables

frame.f_globals

The dictionary used by the frame to look up global variables

frame.f_builtins

The dictionary used by the frame to look up built-in (intrinsic) names

frame.f_lasti

The «precise instruction» of the frame object (this is an index into the bytecode string of the code object)

3.2.13.2.2. Special writable attributes
frame.f_trace

If not None, this is a function called for various events during code execution (this is used by debuggers). Normally an event is triggered for each new source line (see f_trace_lines).

frame.f_trace_lines

Set this attribute to False to disable triggering a tracing event for each source line.

frame.f_trace_opcodes

Set this attribute to True to allow per-opcode events to be requested. Note that this may lead to undefined interpreter behaviour if exceptions raised by the trace function escape to the function being traced.

frame.f_lineno

The current line number of the frame – writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to this attribute.

3.2.13.2.3. Frame object methods

Objetos de marco soportan un método:

frame.clear()

This method clears all references to local variables held by the frame. Also, if the frame belonged to a generator, the generator is finalized. This helps break reference cycles involving frame objects (for example when catching an exception and storing its traceback for later use).

RuntimeError es lanzado si el marco se encuentra en ejecución.

Nuevo en la versión 3.4.

3.2.13.3. Objetos de seguimiento de pila (traceback)

Traceback objects represent the stack trace of an exception. A traceback object is implicitly created when an exception occurs, and may also be explicitly created by calling types.TracebackType.

Distinto en la versión 3.7: Traceback objects can now be explicitly instantiated from Python code.

For implicitly created tracebacks, when the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section La sentencia try.) It is accessible as the third item of the tuple returned by sys.exc_info(), and as the __traceback__ attribute of the caught exception.

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.

For explicitly created tracebacks, it is up to the creator of the traceback to determine how the tb_next attributes should be linked to form a full stack trace.

Special read-only attributes:

traceback.tb_frame

Points to the execution frame of the current level.

Accessing this attribute raises an auditing event object.__getattr__ with arguments obj and "tb_frame".

traceback.tb_lineno

Gives the line number where the exception occurred

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

traceback.tb_next

The special writable attribute 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.

Distinto en la versión 3.7: This attribute is now writable

3.2.13.4. Objetos de segmento (Slice objects)

Slice objects are used to represent slices for __getitem__() methods. They are also created by the built-in slice() function.

Atributos especiales de solo lectura: start es el límite inferior; stop es el límite superior; step es el valor de paso; cada uno es None si es omitido. Estos atributos pueden ser de cualquier tipo.

Los objetos de segmento soportan un método:

slice.indices(self, length)

Este método toma un argumento length de entero simple y calcula información relacionada con el segmento que el mismo describiría si fuera aplicado a una secuencia de elementos length. Retorna una tupla de tres enteros; respectivamente estos son los índices start y stop y el step o longitud del paso del segmento. Índices faltantes o fuera de los límites son manipulados de manera consistente con segmentos regulares.

3.2.13.5. Objetos de método estático

Los objetos de método estático proveen una forma de anular la transformación de objetos de función a objetos de método descritos anteriormente. Un objeto de método estático es una envoltura (wrapper) alrededor de cualquier otro objeto, usualmente un objeto de método definido por usuario. Cuando un objeto de método estático es obtenido desde una clase o una instancia de clase, usualmente el objeto retornado es el objeto envuelto, el cual no está objeto a ninguna transformación adicional. Los objetos de método estático también pueden ser llamados. Los objetos de método estático son creados por el constructor incorporado staticmethod().

3.2.13.6. Objetos de método de clase

A class method object, like a static method object, is a wrapper around another object that alters the way in which that object is retrieved from classes and class instances. The behaviour of class method objects upon such retrieval is described above, under «instance methods». Class method objects are created by the built-in classmethod() constructor.

3.3. Nombres especiales de método

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators. For instance, if a class defines a method named __getitem__(), and x is an instance of this class, then x[i] is roughly equivalent to type(x).__getitem__(x, i). Except where mentioned, attempts to execute an operation raise an exception when no appropriate method is defined (typically AttributeError or TypeError).

Setting a special method to None indicates that the corresponding operation is not available. For example, if a class sets __iter__() to None, the class is not iterable, so calling iter() on its instances will raise a TypeError (without falling back to __getitem__()). [2]

Cuando se implementa una clase que emula cualquier tipo incorporado, es importante que la emulación solo sea implementado al grado que hace sentido para el objeto que está siendo modelado. Por ejemplo, algunas secuencias pueden trabajar bien con la obtención de elementos individuales, pero extraer un segmento puede no tener mucho sentido. (Un ejemplo de esto es la interfaz NodeList, en el Modelo de Objetos del Documento del W3C.)

3.3.1. Personalización básica

object.__new__(cls[, ...])

Es llamado para crear una nueva instancia de clase cls. __new__() es un método estático (como un caso especial, así que no se necesita declarar como tal) que toma la clase de donde fue solicitada una instancia como su primer argumento. Los argumentos restantes son aquellos que se pasan a la expresión del constructor de objetos (para llamar a la clase). El valor retornado de __new__() deberá ser la nueva instancia de objeto (normalmente una instancia de cls).

Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super().__new__(cls[, ...]) with appropriate arguments and then modifying the newly created instance as necessary before returning it.

Si __new__() es invocado durante la construcción del objeto y éste retorna una instancia de cls, entonces el nuevo método __init__() de la instancia será invocado como __init__(self[, …]), donde self es la nueva instancia y los argumentos restantes son iguales a como fueron pasados hacia el constructor de objetos.

Si __new__() no retorna una instancia de cls, entonces el nuevo método __init__() de la instancia no será invocado.

__new__() es destinado principalmente para permitir a subclases de tipos inmutables (como int, str, o tuple) personalizar la creación de instancias. También es comúnmente anulado en metaclases personalizadas con el fin de personalizar la creación de clase.

object.__init__(self[, ...])

Llamado después de que la instancia ha sido creada (por __new__()), pero antes es retornada a quien produce la llamada. Los argumentos son aquellos pasados a la expresión del constructor de la clase. Si una clase base tiene un método __init__(), el método __init__() de clase derivada, de existir, debe llamarlo explícitamente para asegurar la inicialización apropiada de la clase base que es parte de la instancia; por ejemplo: super().__init__([args…]).

Debido a que __new__() y __init__() trabajan juntos construyendo objetos (__new__() para crearlo y __init__() para personalizarlo), ningún valor distinto a None puede ser retornado por __init__(); hacer esto puede causar que se lance una excepción TypeError en tiempo de ejecución.

object.__del__(self)

Llamado cuando la instancia es a punto de ser destruida. Esto también es llamado finalizador o (indebidamente) destructor. Si una clase base tiene un método __del__() el método __del__() de la clase derivada, de existir, debe llamarlo explícitamente para asegurar la eliminación adecuada de la parte de la clase base de la instancia.

Es posible (¡aunque no recomendable!) para el método __del__() posponer la destrucción de la instancia al crear una nueva referencia hacia ésta. Esto es llamado resurrección de objeto. Es dependiente de la implementación si __del__() es llamado una segunda vez cuando un objeto resucitado está por ser destruido; la implementación CPython actual únicamente lo llama una vez.

No está garantizado que los métodos __del__() sean llamados para objetos que aún existen cuando el intérprete se cierra.

Nota

del x no llama directamente x.__del__() — el primero disminuye el conteo de referencia para x uno por uno, y el segundo es llamado únicamente cuando el conteo de referencias de x llega a cero.

Detalles de implementación de CPython: It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the cyclic garbage collector. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback.

Ver también

Documentación para el módulo gc.

Advertencia

Debido a las circunstancias inciertas bajo las que los métodos __del__() son invocados, las excepciones que ocurren durante su ejecución son ignoradas, y una advertencia es mostrada hacia sys.stderr. En particular:

  • __del__() puede ser invocado cuando código arbitrario es ejecutado, incluyendo el de cualquier hilo arbitrario. Si __del__() necesita realizar un cierre de exclusión mutua (lock) o invocar cualquier otro recurso que lo esté bloqueando, podría provocar un bloqueo muto (deadlock) ya que el recurso podría estar siendo utilizado por el código que se interrumpe al ejecutar __del__().

  • __del__() puede ser ejecutado durante el cierre del intérprete. Como consecuencia, las variables globales que necesita para acceder (incluyendo otros módulos) podrían haber sido borradas o establecidas a None. Python garantiza que los globales cuyo nombre comienza con un guión bajo simple sean borrados de su módulo antes que los globales sean borrados; si no existen otras referencias a dichas globales, esto puede ayudar asegurando que los módulos importados aún se encuentren disponibles al momento de llamar al método __del__().

object.__repr__(self)

Llamado por la función incorporada repr() para calcular la cadena “oficial” de representación de un objeto. Si es posible, esto debería verse como una expresión de Python válida que puede ser utilizada para recrear un objeto con el mismo valor (bajo el ambiente adecuado). Si no es posible, una cadena con la forma <…some useful description…> debe ser retornada. El valor de retorno debe ser un objeto de cadena (string). Si una clase define __repr__() pero no __str__(), entonces __repr__() también es utilizado cuando una cadena “informal” de representación de instancias de esas clases son requeridas.

Esto es típicamente utilizado para depurar, así que es importante que la representación sea rica en información e inequívoca.

object.__str__(self)

Llamado por str(object) y las funciones incorporadas format() y print() para calcular la “informal” o bien mostrada cadena de representación de un objeto. El valor de retorno debe ser un objeto string.

Este método difiere de object.__repr__() en que no hay expectativas de que __str__() retorne una expresión de Python válida: una representación más conveniente o concisa pueda ser utilizada.

La implementación por defecto definida por el tipo incorporado object llama a object.__repr__().

object.__bytes__(self)

Llamado por bytes para calcular la representación de la cadena de byte de un objeto. Este deberá retornar un objeto bytes.

object.__format__(self, format_spec)

Llamado por la función incorporada format(), y por extensión, la evaluación de formatted string literals y el método str.format(), para producir la representación “formateada” de un objeto. El argumento format_spec es una cadena que contiene una descripción de las opciones de formato deseadas. La interpretación del argumento format_spec depende del tipo que implementa __format__(), sin embargo, ya sea que la mayoría de las clases deleguen el formato a uno de los tipos incorporados, o utilicen una sintaxis de opción de formato similar.

Ver Especificación de formato Mini-Lenguaje para una descripción de la sintaxis de formato estándar.

El valor de retorno debe ser un objeto de cadena.

Distinto en la versión 3.4: El método __format__ del mismo object lanza un TypeError si se la pasa una cadena no vacía.

Distinto en la versión 3.7: object.__format__(x, ‘’) es ahora equivalente a str(x) en lugar de format(str(self), ‘’).

object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)

Estos son los llamados métodos de comparación rich. La correspondencia entre símbolos de operador y los nombres de método es de la siguiente manera: x<y llama x.__lt__(y), x<=y llama x.__le__(y), x==y llama x.__eq__(y), x!=y llama x.__ne__(y), x>y llama x.__gt__(y), y x>=y llama x.__ge__(y).

A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments. By convention, False and True are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an if statement), Python will call bool() on the value to determine if the result is true or false.

By default, object implements __eq__() by using is, returning NotImplemented in the case of a false comparison: True if x is y else NotImplemented. For __ne__(), by default it delegates to __eq__() and inverts the result unless it is NotImplemented. There are no other implied relationships among the comparison operators or default implementations; for example, the truth of (x<y or x==y) does not imply x<=y. To automatically generate ordering operations from a single root operation, see functools.total_ordering().

Ver el párrafo sobre __hash__() para más notas importantes sobre la creación de objetos hashable que soportan operaciones de comparación personalizadas y son utilizables como llaves de diccionario.

There are no swapped-argument versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other’s reflection, __le__() and __ge__() are each other’s reflection, and __eq__() and __ne__() are their own reflection. If the operands are of different types, and the right operand’s type is a direct or indirect subclass of the left operand’s type, the reflected method of the right operand has priority, otherwise the left operand’s method has priority. Virtual subclassing is not considered.

When no appropriate method returns any value other than NotImplemented, the == and != operators will fall back to is and is not, respectively.

object.__hash__(self)

Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. The __hash__() method should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to mix together the hash values of the components of the object that also play a part in comparison of objects by packing them into a tuple and hashing the tuple. Example:

def __hash__(self):
    return hash((self.name, self.nick, self.color))

Nota

hash() trunca el valor retornado del método personalizado __hash__() del objeto al tamaño de Py_ssize_t. Esto normalmente son 8 bytes en estructuras de 64-bits y 4 bytes en estructuras de 32 bits. Si el __hash__() de un objeto debe interoperar en estructuras de tamaños de bits diferentes, asegúrese de revisar la amplitud en todas las estructuras soportadas. Una forma fácil de hacer esto es con python -c “import sys; print(sys.hash_info.width)”.

If a class does not define an __eq__() method it should not define a __hash__() operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

Clases definidas por usuario tienen los métodos __eq__() y __hash__() por defecto; con ellos, todos los objetos se comparan de manera desigual (excepto con ellos mismos) y x.__hash__() retorna un valor apropiado tal que x == y implique que x es y y hash(x) == hash(y).

Una clase que anula __eq__() y no define __hash__() tendrá implícito su __hash__() establecido a None. Cuando el método __hash__() de una clase es None, instancias de la clase lanzarán un TypeError cuando el programa intente obtener el valor del hash, y también será correctamente identificado como de hash no calculable cuando se verifique isinstance(obj, collections.abc.Hashable).

Si una clase que anula __eq__() necesita conservar la implementación de __hash__() de una clase padre, al intérprete se le debe informar explícitamente estableciendo __hash__ = <ParentClass>.__hash__.

Si una clase que no anula __eq__() desea eliminar el soporte de hash, debe incluir __hash__ = None en la definición de clase. Una clase que define su propio __hash__() y que explícitamente lanza un TypeError será identificado de manera incorrecta como de hash calculable por una llamada isinstance(obj, collections.abc.Hashable).

Nota

Por defecto los valores de objetos str y bytes de __hash__() son “salados” con un valor aleatorio impredecible. Aunque se mantienen constantes dentro de un proceso Python particular, no son predecibles entre invocaciones repetidas de Python.

This is intended to provide protection against a denial-of-service caused by carefully chosen inputs that exploit the worst case performance of a dict insertion, O(n2) complexity. See http://ocert.org/advisories/ocert-2011-003.html for details.

Cambiar los valores hash afectan el orden de la iteración de los sets. Python nunca ha dado garantías en relación a este orden (y típicamente varía entre estructuras de 32-bits y 64-bits).

Ver también PYTHONHASHSEED.

Distinto en la versión 3.3: La aleatorización de hash es habilitada por defecto.

object.__bool__(self)

Called to implement truth value testing and the built-in operation bool(); should return False or True. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true.

3.3.2. Personalizando acceso a atributos

Los siguientes métodos pueden ser definidos para personalizar el significado de acceso a atributos (uso de, asignación a, o borrado de x.name) para instancias de clase.

object.__getattr__(self, name)

Es llamado cuando el acceso a atributos por defecto falla con un AttributeError (ya sea que __getattribute__() lanza una excepción AttributeError porque name no es un atributo de instancia o un atributo en el árbol de clase para self; o el __get__() de la propiedad de name lanza una excepción AttributeError). Este método debe retornar el valor de atributo (calculado) o lanzar una excepción AttributeError.

Tome en cuenta que si el atributo es encontrado a través del mecanismo normal, __getattr__() no es llamado. (Esto es una desigualdad intencional entre __getattr__() y __setattr__().) Esto es realizado tanto por motivos de eficiencia y porque de otra manera __getattr__() no tendría manera de acceder a otros atributos de la instancia. Tome en cuenta que al menos para variables de instancia, se puede fingir control total al no insertar ningún valor en el diccionario de atributo de instancia (sino insertándolos en otro objeto). Ver el método __getattribute__() a continuación para una forma de tener control total sobre el acceso de atributo.

object.__getattribute__(self, name)

Es llamado incondicionalmente para implementar acceso de atributo por instancias de clase. Si la clase también define __getattr__(), éste no será llamado a menos que __getattribute__() lo llame de manera explícita o lance una excepción AttributeError. Este método deberá retornar el valor de atributo (calculado) o lanzar una excepción AttributeError. Para evitar la recursividad infinita en este método, su implementación deberá siempre llamar al método de la clase base con el mismo nombre para acceder cualquier atributo que necesite, por ejemplo, object.__getattribute__(self, name).

Nota

This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Búsqueda de método especial.

Lanza un evento de auditoría object.__getattr__ con argumentos obj, name.

object.__setattr__(self, name, value)

Es llamado cuando se intenta la asignación de atributos. Éste es llamado en lugar del mecanismo normal (p. ej. guardar el valor en el diccionario de instancias). name es el nombre de atributo, value es el valor que se le asigna.

Si __setattr__() quiere asignar a un atributo de instancia, debe llamar al método de la clase base con el mismo nombre, por ejemplo, object.__setattr__(self, name, value).

Lanza un evento de auditoría object.__setattr__ con argumentos obj, name, value.

object.__delattr__(self, name)

Al igual que __setattr__() pero para borrado de atributos en lugar de establecerlos. Esto solo de ser implementado si del obj.name es significativo para el objeto.

Lanza un evento de auditoría object.__delattr__ con argumentos obj, name.

object.__dir__(self)

Called when dir() is called on the object. An iterable must be returned. dir() converts the returned iterable to a list and sorts it.

3.3.2.1. Personalizando acceso a atributos de módulo

Nombres especiales __getattr__ y __dir__ también pueden ser utilizados para personalizar acceso a atributos de módulo. La función __getattr__ a nivel del módulo debe aceptar un argumento que es el nombre del atributo y retornar el valor calculado o lanzar una excepción AttributeError. Si un atributo no es encontrado en el objeto de módulo a través de una búsqueda normal, p. ej. object.__getattribute__(), entonces __getattr__ es buscado en el módulo __dict__ antes de lanzar una excepción AttributeError. Si es encontrado, es llamado con el nombre de atributo y el resultado es retornado.

The __dir__ function should accept no arguments, and return an iterable of strings that represents the names accessible on module. If present, this function overrides the standard dir() search on a module.

Para una personalización más precisa sobre el comportamiento del módulo (estableciendo atributos, propiedades, etc.), se puede establecer el atributo __class__ de un objeto de módulo a una subclase de types.ModuleType. Por ejemplo:

import sys
from types import ModuleType

class VerboseModule(ModuleType):
    def __repr__(self):
        return f'Verbose {self.__name__}'

    def __setattr__(self, attr, value):
        print(f'Setting {attr}...')
        super().__setattr__(attr, value)

sys.modules[__name__].__class__ = VerboseModule

Nota

Definiendo un módulo __getattr__ y estableciendo un módulo __class__ solo afecta búsquedas que utilizan la sintaxis de acceso a atributo – acceder directamente a las globales del módulo (ya sea por código dentro del módulo, o a través de una referencia al diccionario de globales del módulo) no se ve afectado.

Distinto en la versión 3.5: El atributo de módulo __class__ es ahora escribible.

Nuevo en la versión 3.7: Atributos de módulo __getattr__ y __dir__.

Ver también

PEP 562 - Módulos __getattr__ y __dir__

Describe las funciones __getattr__ y __dir__ en módulos.

3.3.2.2. Implementando descriptores

Los siguientes métodos solo aplican cuando una instancia de clase que contiene el método (llamado clase descriptora) aparece en una clase propietaria (el descriptor debe estar en el diccionario de clase del propietario o en el diccionario de clase de alguno de sus padres). En los ejemplos a continuación, “el atributo” se refiere al atributo cuyo nombre es la llave de la propiedad en la clase propietaria __dict__.

object.__get__(self, instance, owner=None)

Es llamado para obtener el atributo de la clase propietaria (acceso a atributos de clase) o de una instancia de dicha clase (acceso a atributos de instancia). El argumento opcional owner es la clase propietaria, mientras que instance es la instancia a través de la cual el atributo fue accedido, o None cuando el atributo es accedido a través de owner.

Este método debe retornar el valor de atributo calculado o lanzar una excepción AttributeError.

PEP 252 especifica que __get__() puede ser llamado con uno o dos argumentos. Los propios descriptores incorporados de Python soportan esta especificación; sin embargo, es probable que algunas herramientas de terceros tengan descriptores que requieran ambos argumentos. La propia implementación de __getattribute__() en Python siempre pasa ambos argumentos si son requeridos o no.

object.__set__(self, instance, value)

Es llamado para establecer el atributo en una instancia instance de la clase propietaria a un nuevo valor value.

Nota, agregar __set__() o __delete__() cambia el tipo de descriptor a un “descriptor de datos”. Ver Invocando descriptores para más detalles.

object.__delete__(self, instance)

Es llamado para borrar el atributo en una instancia instance de la clase propietaria.

Instances of descriptors may also have the __objclass__ attribute present:

object.__objclass__

The attribute __objclass__ is interpreted by the inspect module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C).

3.3.2.3. Invocando descriptores

In general, a descriptor is an object attribute with «binding behavior», one whose attribute access has been overridden by methods in the descriptor protocol: __get__(), __set__(), and __delete__(). If any of those methods are defined for an object, it is said to be a descriptor.

El comportamiento por defecto para atributos de acceso es obtener (get), establecer (set) o borrar (delete) el atributo del diccionario del objeto. Por ejemplo, a.x tiene una cadena de búsqueda que comienza con a.__dict__[‘x’], luego type(a).__dict__[‘x’], y continúa por las clases base de type(a) excluyendo metaclases.

Sin embargo, si el valor buscado es un objeto definiendo uno de los métodos del descriptor, entonces Python puede anular el comportamiento por defecto e invocar al método del descriptor en su lugar. Dónde ocurre esto en la cadena de precedencia depende de qué métodos de descriptor fueron definidos y cómo son llamados.

El punto de inicio por invocación de descriptor es un enlace a.x. Cómo los argumentos son ensamblados dependen de a:

Llamado directo

El llamado más simple y menos común es cuando el código de usuario invoca directamente un método descriptor: x.__get__(a).

Enlace de instancia

Al enlazar a una instancia de objeto, a es transformado en un llamado: type(a).__dict__[‘x’].__get__(a, type(a)).

Enlace de clase

Al enlazar a una clase, A.x es transformado en un llamado: A.__dict__[‘x’].__get__(None, A).

Súper enlace

A dotted lookup such as super(A, a).x searches a.__class__.__mro__ for a base class B following A and then returns B.__dict__['x'].__get__(a, A). If not a descriptor, x is returned unchanged.

For instance bindings, the precedence of descriptor invocation depends on which descriptor methods are defined. A descriptor can define any combination of __get__(), __set__() and __delete__(). If it does not define __get__(), then accessing the attribute will return the descriptor object itself unless there is a value in the object’s instance dictionary. If the descriptor defines __set__() and/or __delete__(), it is a data descriptor; if it defines neither, it is a non-data descriptor. Normally, data descriptors define both __get__() and __set__(), while non-data descriptors have just the __get__() method. Data descriptors with __get__() and __set__() (and/or __delete__()) defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances.

Python methods (including those decorated with @staticmethod and @classmethod) are implemented as non-data descriptors. Accordingly, instances can redefine and override methods. This allows individual instances to acquire behaviors that differ from other instances of the same class.

La función property() es implementada como un descriptor de datos. Por lo tanto, las instancias no pueden anular el comportamiento de una propiedad.

3.3.2.4. __slots__

__slots__ allow us to explicitly declare data members (like properties) and deny the creation of __dict__ and __weakref__ (unless explicitly declared in __slots__ or available in a parent.)

The space saved over using __dict__ can be significant. Attribute lookup speed can be significantly improved as well.

object.__slots__

This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. __slots__ reserves space for the declared variables and prevents the automatic creation of __dict__ and __weakref__ for each instance.

Notes on using __slots__:

  • When inheriting from a class without __slots__, the __dict__ and __weakref__ attribute of the instances will always be accessible.

  • Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition. Attempts to assign to an unlisted variable name raises AttributeError. If dynamic assignment of new variables is desired, then add '__dict__' to the sequence of strings in the __slots__ declaration.

  • Without a __weakref__ variable for each instance, classes defining __slots__ do not support weak references to its instances. If weak reference support is needed, then add '__weakref__' to the sequence of strings in the __slots__ declaration.

  • __slots__ are implemented at the class level by creating descriptors for each variable name. As a result, class attributes cannot be used to set default values for instance variables defined by __slots__; otherwise, the class attribute would overwrite the descriptor assignment.

  • The action of a __slots__ declaration is not limited to the class where it is defined. __slots__ declared in parents are available in child classes. However, child subclasses will get a __dict__ and __weakref__ unless they also define __slots__ (which should only contain names of any additional slots).

  • Si una clase define un espacio (slot) también definido en una clase base, la variable de instancia definida por el espacio de la clase base es inaccesible (excepto al obtener su descriptor directamente de la clase base). Esto hace que el significado del programa sea indefinido. En el futuro se podría agregar una verificación para prevenir esto.

  • TypeError will be raised if nonempty __slots__ are defined for a class derived from a "variable-length" built-in type such as int, bytes, and tuple.

  • Any non-string iterable may be assigned to __slots__.

  • If a dictionary is used to assign __slots__, the dictionary keys will be used as the slot names. The values of the dictionary can be used to provide per-attribute docstrings that will be recognised by inspect.getdoc() and displayed in the output of help().

  • __class__ assignment works only if both classes have the same __slots__.

  • Multiple inheritance with multiple slotted parent classes can be used, but only one parent is allowed to have attributes created by slots (the other bases must have empty slot layouts) - violations raise TypeError.

  • If an iterator is used for __slots__ then a descriptor is created for each of the iterator’s values. However, the __slots__ attribute will be an empty iterator.

3.3.3. Personalización de creación de clases

Whenever a class inherits from another class, __init_subclass__() is called on the parent class. This way, it is possible to write classes which change the behavior of subclasses. This is closely related to class decorators, but where class decorators only affect the specific class they’re applied to, __init_subclass__ solely applies to future subclasses of the class defining the method.

classmethod object.__init_subclass__(cls)

Este método es llamado siempre que la clase que lo contiene sea heredada. cls es entonces, la nueva subclase. Si se define como un método de instancia normal, éste es convertido de manera implícita a un método de clase.

Keyword arguments which are given to a new class are passed to the parent class’s __init_subclass__. For compatibility with other classes using __init_subclass__, one should take out the needed keyword arguments and pass the others over to the base class, as in:

class Philosopher:
    def __init_subclass__(cls, /, default_name, **kwargs):
        super().__init_subclass__(**kwargs)
        cls.default_name = default_name

class AustralianPhilosopher(Philosopher, default_name="Bruce"):
    pass

La implementación por defecto object.__init_subclass__ no hace nada, pero lanza un error si es llamado con cualquier argumento.

Nota

La sugerencia de metaclase metaclass es consumido por el resto de la maquinaria de tipos, y nunca se pasa a las implementaciones __init_subclass__. La clase meta actual (más que la sugerencia explícita) puede ser accedida como type(cls).

Nuevo en la versión 3.6.

When a class is created, type.__new__() scans the class variables and makes callbacks to those with a __set_name__() hook.

object.__set_name__(self, owner, name)

Llamado automáticamente al momento en el que se crea la clase propietaria owner. El objeto es asignado a name en esa clase:

class A:
    x = C()  # Automatically calls: x.__set_name__(A, 'x')

Si la variable de clase se asigna después de crear la clase, __set_name__() no se llamará automáticamente. Si es necesario, __set_name__() se puede llamar directamente:

class A:
   pass

c = C()
A.x = c                  # The hook is not called
c.__set_name__(A, 'x')   # Manually invoke the hook

Ver Creando el objeto de clase para más detalles.

Nuevo en la versión 3.6.

3.3.3.1. Metaclases

Por defecto, las clases son construidas usando type(). El cuerpo de la clase es ejecutado en un nuevo espacio de nombres y el nombre de la clase es ligado de forma local al resultado de type(name, bases, namespace).

El proceso de creación de clase puede ser personalizado pasando el argumento de palabra clave metaclass en la línea de definición de la clase, o al heredar de una clase existente que incluya dicho argumento. En el siguiente ejemplo, ambos MyClass y MySubclass son instancias de Meta:

class Meta(type):
    pass

class MyClass(metaclass=Meta):
    pass

class MySubclass(MyClass):
    pass

Cualquier otro argumento de palabra clave que sea especificado en la definición de clase es pasado mediante todas las operaciones de metaclase descritas a continuación.

Cuando una definición de clase es ejecutada, los siguientes pasos ocurren:

  • Entradas de la orden de resolución de método (MRU) son resueltas;

  • se determina la metaclase adecuada;

  • se prepara el espacio de nombres de clase;

  • se ejecuta el cuerpo de la clase;

  • se crea el objeto de clase.

3.3.3.2. Resolviendo entradas de la Orden de Resolución de Métodos (MRU)

object.__mro_entries__(self, bases)

If a base that appears in a class definition is not an instance of type, then an __mro_entries__() method is searched on the base. If an __mro_entries__() method is found, the base is substituted with the result of a call to __mro_entries__() when creating the class. The method is called with the original bases tuple passed to the bases parameter, and must return a tuple of classes that will be used instead of the base. The returned tuple may be empty: in these cases, the original base is ignored.

Ver también

types.resolve_bases()

Dynamically resolve bases that are not instances of type.

PEP 560

Core support for typing module and generic types.

3.3.3.3. Determinando la metaclase adecuada

La metaclase adecuada para la definición de una clase es determinada de la siguiente manera:

  • si no se dan bases ni metaclases explícitas, entonces se utiliza type();

  • si se da una metaclase explícita y no es una instancia de type(), entonces se utiliza directamente como la metaclase;

  • si se da una instancia de type() como la metaclase explícita, o se definen bases, entonces se utiliza la metaclase más derivada.

La metaclase más derivada es elegida de la metaclase especificada explícitamente (si existe) y de la metaclase (p. ej. type(cls)) de todas las clases base especificadas.

3.3.3.4. Preparando el espacio de nombres de la clase

Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a __prepare__ attribute, it is called as namespace = metaclass.__prepare__(name, bases, **kwds) (where the additional keyword arguments, if any, come from the class definition). The __prepare__ method should be implemented as a classmethod. The namespace returned by __prepare__ is passed in to __new__, but when the final class object is created the namespace is copied into a new dict.

Si la metaclase no tiene atributo __prepare__, entonces el espacio de nombres de clase es iniciado como un mapeo vacío ordenado.

Ver también

PEP 3115 - Metaclases en Python 3000

Introduce el enlace de espacio de nombres __prepare__

3.3.3.5. Ejecutando el cuerpo de la clase

El cuerpo de la clase es ejecutado como exec(body, globals(), namespace) (aproximadamente). La diferencia clave con un llamado normal a exec() es que el alcance léxico permite que el cuerpo de la clase (incluyendo cualquier método) haga referencia a nombres de los alcances actuales y externos cuando la definición de clase sucede dentro de la función.

Sin embargo, aún cuando la definición de clase sucede dentro de la función, los métodos definidos dentro de la clase aún no pueden ver nombres definidos dentro del alcance de la clase. Variables de clase deben ser accedidas a través del primer parámetro de instancia o métodos de clase, o a través de la referencia al léxico implícito __class__ descrita en la siguiente sección.

3.3.3.6. Creando el objeto de clase

Una vez que el espacio de nombres de la clase ha sido poblado al ejecutar el cuerpo de la clase, el objeto de clase es creado al llamar metaclass(name, bases, namespace, **kwds) (las palabras clave adicionales que se pasan aquí, son las mismas que aquellas pasadas en __prepare__).

Este objeto de clase es el que será referenciado por la forma sin argumentos de super(). __class__ es una referencia de cierre implícita creada por el compilador si cualquier método en el cuerpo de una clase se refiere tanto a __class__ o super. Esto permite que la forma sin argumentos de super() identifique correctamente la clase definida en base al alcance léxico, mientras la clase o instancia que fue utilizada para hacer el llamado actual es identificado en base al primer argumento que se pasa al método.

Detalles de implementación de CPython: En CPython 3.6 y posterior, la celda __class__ se pasa a la metaclase como una entrada __classcell__ en el espacio de nombres de la clase. En caso de existir, esto debe ser propagado hacia el llamado type.__new__ para que la clase se inicie correctamente. No hacerlo resultará en un error RuntimeError en Python 3.8.

Cuando se utiliza la metaclase por defecto type, o cualquier metaclase que finalmente llama a type.__new__, los siguientes pasos de personalización adicional son invocados después de crear el objeto de clase:

  1. El método type.__new__ recolecta todos los atributos en el espacio de nombres de la clase que definen un método __set_name__();

  2. Esos métodos __set_name__ son llamados con la clase siendo definida y el nombre de ese atributo particular asignado;

  3. El gancho __init_subclass__() llama al padre inmediato de la nueva clase en su orden de resolución del método.

Después de que el objeto de clase es creado, se pasa al decorador de clase incluido en su definición (si existe) y el objeto resultante es enlazado en el espacio de nombres local como la clase definida.

Cuando una nueva clase es creada por type.__new__, el objeto proporcionado como el parámetro de espacio de nombres es copiado a un trazado ordenado y el objeto original es descartado. La nueva copia es envuelta en un proxy de solo lectura, que se convierte en el atributo __dict__ del objeto de clase.

Ver también

PEP 3135 - Nuevo súper

Describe la referencia de cierre implícita __class__

3.3.3.7. Usos para metaclases

Los usos potenciales para metaclases son ilimitados. Algunas ideas que ya han sido exploradas incluyen enumeración, registros, revisión de interface, delegación automática, creación de propiedades automática, proxy, infraestructuras, y bloqueo/sincronización automática de recursos.

3.3.4. Personalizando revisiones de instancia y subclase

Los siguientes métodos son utilizados para anular el comportamiento por defecto de las funciones incorporadas isinstance() y issubclass().

En particular, la metaclase abc.ABCMeta implementa estos métodos para permitir la adición de Clases Base Abstractas (ABCs, por su nombre en inglés Abstract Base Clases) como “clases base virtuales” a cualquier clase o tipo (incluyendo tipos incorporados), incluyendo otros ABCs.

class.__instancecheck__(self, instance)

Retorna true si la instancia instance debe ser considerada una instancia (directa o indirecta) de clase class. De ser definida, es llamado para implementar isinstance(instance, class).

class.__subclasscheck__(self, subclass)

Retorna true si la subclase subclass debe ser considerada una subclase (directa o indirecta) de clase class. De ser definida, es llamado para implementar issubclass(subclass, class).

Tome en cuenta que estos métodos son buscados en el tipo (metaclase) de una clase. No pueden ser definidos como métodos de clase en la clase actual. Esto es consistente con la búsqueda de métodos especiales que son llamados en instancias, solo en este caso la instancia es por sí misma una clase.

Ver también

PEP 3119 - Introducción a Clases Base Abstractas (Abstract Base Classes)

Incluye la especificación para personalizar el comportamiento de isinstance() y issubclass() a través de __instancecheck__() y __subclasscheck__(), con motivación para esta funcionalidad en el contexto de agregar Clases Base Abstractas (ver el módulo abc) al lenguaje.

3.3.5. Emulando tipos genéricos

When using type annotations, it is often useful to parameterize a generic type using Python’s square-brackets notation. For example, the annotation list[int] might be used to signify a list in which all the elements are of type int.

Ver también

PEP 484 - Type Hints

Introducing Python’s framework for type annotations

Generic Alias Types

Documentation for objects representing parameterized generic classes

Genéricos, user-defined generics and typing.Generic

Documentation on how to implement generic classes that can be parameterized at runtime and understood by static type-checkers.

A class can generally only be parameterized if it defines the special class method __class_getitem__().

classmethod object.__class_getitem__(cls, key)

Retornar un objeto representando la especialización de una clase genérica por argumentos de tipo encontrados en key.

When defined on a class, __class_getitem__() is automatically a class method. As such, there is no need for it to be decorated with @classmethod when it is defined.

3.3.5.1. The purpose of __class_getitem__

The purpose of __class_getitem__() is to allow runtime parameterization of standard-library generic classes in order to more easily apply type hints to these classes.

To implement custom generic classes that can be parameterized at runtime and understood by static type-checkers, users should either inherit from a standard library class that already implements __class_getitem__(), or inherit from typing.Generic, which has its own implementation of __class_getitem__().

Custom implementations of __class_getitem__() on classes defined outside of the standard library may not be understood by third-party type-checkers such as mypy. Using __class_getitem__() on any class for purposes other than type hinting is discouraged.

3.3.5.2. __class_getitem__ versus __getitem__

Usually, the subscription of an object using square brackets will call the __getitem__() instance method defined on the object’s class. However, if the object being subscribed is itself a class, the class method __class_getitem__() may be called instead. __class_getitem__() should return a GenericAlias object if it is properly defined.

Presented with the expression obj[x], the Python interpreter follows something like the following process to decide whether __getitem__() or __class_getitem__() should be called:

from inspect import isclass

def subscribe(obj, x):
    """Return the result of the expression 'obj[x]'"""

    class_of_obj = type(obj)

    # If the class of obj defines __getitem__,
    # call class_of_obj.__getitem__(obj, x)
    if hasattr(class_of_obj, '__getitem__'):
        return class_of_obj.__getitem__(obj, x)

    # Else, if obj is a class and defines __class_getitem__,
    # call obj.__class_getitem__(x)
    elif isclass(obj) and hasattr(obj, '__class_getitem__'):
        return obj.__class_getitem__(x)

    # Else, raise an exception
    else:
        raise TypeError(
            f"'{class_of_obj.__name__}' object is not subscriptable"
        )

In Python, all classes are themselves instances of other classes. The class of a class is known as that class’s metaclass, and most classes have the type class as their metaclass. type does not define __getitem__(), meaning that expressions such as list[int], dict[str, float] and tuple[str, bytes] all result in __class_getitem__() being called:

>>> # list has class "type" as its metaclass, like most classes:
>>> type(list)
<class 'type'>
>>> type(dict) == type(list) == type(tuple) == type(str) == type(bytes)
True
>>> # "list[int]" calls "list.__class_getitem__(int)"
>>> list[int]
list[int]
>>> # list.__class_getitem__ returns a GenericAlias object:
>>> type(list[int])
<class 'types.GenericAlias'>

However, if a class has a custom metaclass that defines __getitem__(), subscribing the class may result in different behaviour. An example of this can be found in the enum module:

>>> from enum import Enum
>>> class Menu(Enum):
...     """A breakfast menu"""
...     SPAM = 'spam'
...     BACON = 'bacon'
...
>>> # Enum classes have a custom metaclass:
>>> type(Menu)
<class 'enum.EnumMeta'>
>>> # EnumMeta defines __getitem__,
>>> # so __class_getitem__ is not called,
>>> # and the result is not a GenericAlias object:
>>> Menu['SPAM']
<Menu.SPAM: 'spam'>
>>> type(Menu['SPAM'])
<enum 'Menu'>

Ver también

PEP 560 - Core Support for typing module and generic types

Introducing __class_getitem__(), and outlining when a subscription results in __class_getitem__() being called instead of __getitem__()

3.3.6. Emulando objetos que se pueden llamar

object.__call__(self[, args...])

Es llamado cuando la instancia es “llamada” como una función; si este método es definido, x(arg1, arg2, …) es una clave corta para x.__call__(arg1, arg2, …).

3.3.7. Emulando tipos de contenedores

The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers k for which 0 <= k < N where N is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods keys(), values(), items(), get(), clear(), setdefault(), pop(), popitem(), copy(), and update() behaving similar to those for Python’s standard dictionary objects. The collections.abc module provides a MutableMapping abstract base class to help create those methods from a base set of __getitem__(), __setitem__(), __delitem__(), and keys(). Mutable sequences should provide methods append(), count(), index(), extend(), insert(), pop(), remove(), reverse() and sort(), like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods __add__(), __radd__(), __iadd__(), __mul__(), __rmul__() and __imul__() described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the __contains__() method to allow efficient use of the in operator; for mappings, in should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the __iter__() method to allow efficient iteration through the container; for mappings, __iter__() should iterate through the object’s keys; for sequences, it should iterate through the values.

object.__len__(self)

Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __bool__() method and whose __len__() method returns zero is considered to be false in a Boolean context.

Detalles de implementación de CPython: In CPython, the length is required to be at most sys.maxsize. If the length is larger than sys.maxsize some features (such as len()) may raise OverflowError. To prevent raising OverflowError by truth value testing, an object must define a __bool__() method.

object.__length_hint__(self)

Called to implement operator.length_hint(). Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer >= 0. The return value may also be NotImplemented, which is treated the same as if the __length_hint__ method didn’t exist at all. This method is purely an optimization and is never required for correctness.

Nuevo en la versión 3.4.

Nota

La segmentación se hace exclusivamente con los siguientes tres métodos. Un llamado como

a[1:2] = b

es traducido a

a[slice(1, 2, None)] = b

etcétera. Elementos faltantes de segmentos siempre son llenados con None.

object.__getitem__(self, key)

Called to implement evaluation of self[key]. For sequence types, the accepted keys should be integers. Optionally, they may support slice objects as well. Negative index support is also optional. If key is of an inappropriate type, TypeError may be raised; if key is a value outside the set of indexes for the sequence (after any special interpretation of negative values), IndexError should be raised. For mapping types, if key is missing (not in the container), KeyError should be raised.

Nota

ciclos for esperan que una excepción IndexError sea lanzada para que índices ilegales permitan la detección adecuada del fin de una secuencia.

Nota

When subscripting a class, the special class method __class_getitem__() may be called instead of __getitem__(). See __class_getitem__ versus __getitem__ for more details.

object.__setitem__(self, key, value)

Es llamado para implementar la asignación a self[key]. Lo mismo con respecto a __getitem__(). Esto solo debe ser implementado para mapeos si los objetos permiten cambios a los valores de las llaves, o si nuevas llaves pueden ser añadidas, o para secuencias si los elementos pueden ser reemplazados. Las mismas excepciones deben ser lanzadas para valores de key inadecuados con respecto al método __getitem__().

object.__delitem__(self, key)

Es llamado para implementar el borrado de self[key]. Lo mismo con respecto a __getitem__(). Esto solo debe ser implementado para mapeos si los objetos permiten el borrado de llaves, o para secuencias si los elementos pueden ser eliminados de la secuencia. Las mismas excepciones deben ser lanzadas por valores de key inapropiados con respecto al método __getitem__().

object.__missing__(self, key)

Es llamado por dict.__getitem__() para implementar self[key] para subclases de diccionarios cuando la llave no se encuentra en el diccionario.

object.__iter__(self)

This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container.

object.__reversed__(self)

Es llamado (si existe) por la función incorporada reversed() para implementar una interacción invertida. Debe retornar un nuevo objeto iterador que itere sobre todos los objetos en el contenedor en orden inverso.

Si el método __reversed__() no es proporcionado, la función incorporada reversed() recurrirá a utilizar el protocolo de secuencia (__len__() y __getitem__()). Objetos que permiten el protocolo de secuencia deben únicamente proporcionar __reversed__() si no pueden proporcionar una implementación que sea más eficiente que la proporcionada por reversed().

Los operadores de prueba de pertenencia (in and not in) son normalmente implementados como una iteración sobre un contenedor. Sin embargo, los objetos de contenedor pueden proveer el siguiente método especial con una implementación más eficiente, que tampoco requiere que el objeto sea iterable.

object.__contains__(self, item)

Es llamado para implementar operadores de prueba de pertenencia. Deben retornar true si item se encuentra en self, de lo contrario false. Para objetos de mapeo, estos debe considerar las llaves del mapeo en lugar de los valores o los pares de llave-valor.

Para objetos que no definen __contains__(), la prueba de pertenencia primero intenta la iteración a través de __iter__(), y luego el antiguo protocolo de iteración de secuencia a través de __getitem__(), ver esta sección en la referencia del lenguaje.

3.3.8. Emulando tipos numéricos

Los siguientes métodos pueden ser definidos para emular objetos numéricos. Métodos que corresponden a operaciones que no son permitidas por el número particular implementado (por ejemplo, operaciones bit a bit para números no enteros) se deben dejar sin definir.

object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
object.__matmul__(self, other)
object.__truediv__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)

These methods are called to implement the binary arithmetic operations (+, -, *, @, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |). For instance, to evaluate the expression x + y, where x is an instance of a class that has an __add__() method, type(x).__add__(x, y) is called. The __divmod__() method should be the equivalent to using __floordiv__() and __mod__(); it should not be related to __truediv__(). Note that __pow__() should be defined to accept an optional third argument if the ternary version of the built-in pow() function is to be supported.

If one of those methods does not support the operation with the supplied arguments, it should return NotImplemented.

object.__radd__(self, other)
object.__rsub__(self, other)
object.__rmul__(self, other)
object.__rmatmul__(self, other)
object.__rtruediv__(self, other)
object.__rfloordiv__(self, other)
object.__rmod__(self, other)
object.__rdivmod__(self, other)
object.__rpow__(self, other[, modulo])
object.__rlshift__(self, other)
object.__rrshift__(self, other)
object.__rand__(self, other)
object.__rxor__(self, other)
object.__ror__(self, other)

These methods are called to implement the binary arithmetic operations (+, -, *, @, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |) with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation [3] and the operands are of different types. [4] For instance, to evaluate the expression x - y, where y is an instance of a class that has an __rsub__() method, type(y).__rsub__(y, x) is called if type(x).__sub__(x, y) returns NotImplemented.

Se debe tomar en cuenta que la función ternaria pow() no intentará llamar a __rpow__() (las reglas de coerción se volverían demasiado complicadas).

Nota

Si el tipo del operando de la derecha es una subclase del tipo del operando de la izquierda y esa subclase proporciona el método reflejado para la operación, este método será llamado antes del método no reflejado del operando izquierdo. Este comportamiento permite que las subclases anulen las operaciones de sus predecesores.

object.__iadd__(self, other)
object.__isub__(self, other)
object.__imul__(self, other)
object.__imatmul__(self, other)
object.__itruediv__(self, other)
object.__ifloordiv__(self, other)
object.__imod__(self, other)
object.__ipow__(self, other[, modulo])
object.__ilshift__(self, other)
object.__irshift__(self, other)
object.__iand__(self, other)
object.__ixor__(self, other)
object.__ior__(self, other)

These methods are called to implement the augmented arithmetic assignments (+=, -=, *=, @=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). If a specific method is not defined, or if that method returns NotImplemented, the augmented assignment falls back to the normal methods. For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . If __iadd__() does not exist, or if x.__iadd__(y) returns NotImplemented, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y. In certain situations, augmented assignment can result in unexpected errors (see ¿Por qué hacer lo siguiente, a_tuple[i] += ['item'], lanza una excepción cuando la suma funciona?), but this behavior is in fact part of the data model.

object.__neg__(self)
object.__pos__(self)
object.__abs__(self)
object.__invert__(self)

Es llamado para implementar las operaciones aritméticas unarias (-, +, abs() and ~).

object.__complex__(self)
object.__int__(self)
object.__float__(self)

Es llamado para implementar las funciones incorporadas complex(), int() y float(). Debe retornar un valor del tipo apropiado.

object.__index__(self)

Es llamado para implementar operator.index(), y cuando sea que Python necesite convertir sin pérdidas el objeto numérico a un objeto entero (tal como en la segmentación o slicing, o las funciones incorporadas bin(), hex() y oct()). La presencia de este método indica que el objeto numérico es un tipo entero. Debe retornar un entero.

Si __int__(), __float__() y __complex__() no son definidos, entonces todas las funciones incorporadas correspondientes int(), float() y complex() vuelven a __index__().

object.__round__(self[, ndigits])
object.__trunc__(self)
object.__floor__(self)
object.__ceil__(self)

Es llamado para implementar la función incorporada round() y las funciones math trunc(), floor() y ceil(). A menos que ndigits sea pasado a __round__() todos estos métodos deben retornar el valor del objeto truncado a Integral (normalmente int).

The built-in function int() falls back to __trunc__() if neither __int__() nor __index__() is defined.

Distinto en la versión 3.11: The delegation of int() to __trunc__() is deprecated.

3.3.9. Gestores de Contexto en la Declaración with

Un context manager es un objeto que define el contexto en tiempo de ejecución a ser establecido cuando se ejecuta una declaración with. El gestor de contexto maneja la entrada y la salida del contexto en tiempo de ejecución deseado para la ejecución del bloque de código. Los gestores de contexto son normalmente invocados utilizando la declaración with (descritos en la sección La sentencia with), pero también pueden ser utilizados al invocar directamente sus métodos.

Usos típicos de los gestores de contexto incluyen guardar y restablecer diversos tipos de declaraciones globales, bloquear y desbloquear recursos, cerrar archivos abiertos, etc.

Para más información sobre gestores de contexto, ver Tipos gestores de contexto.

object.__enter__(self)

Ingresa al contexto en tiempo de ejecución relacionado con este objeto. La declaración with ligará el valor de retorno de este método al objetivo especificado en cláusula as de la declaración, en caso de existir.

object.__exit__(self, exc_type, exc_value, traceback)

Sale del contexto en tiempo de ejecución relacionado a este objeto. Los parámetros describen la excepción que causa la salida del contexto. Si éste se termina sin excepción, los tres argumentos serán None.

Si se proporciona una excepción, y el método desea eliminarla (por ejemplo, prevenir que sea propagada), debe retornar un valor verdadero. De lo contrario, la excepción será procesada de forma normal al salir de este método.

Note that __exit__() methods should not reraise the passed-in exception; this is the caller’s responsibility.

Ver también

PEP 343 - La declaración “with”

La especificación, el antecedente, y los ejemplos para la declaración de Python with.

3.3.10. Personalización de argumentos posicionales en la coincidencia de patrones de clase

When using a class name in a pattern, positional arguments in the pattern are not allowed by default, i.e. case MyClass(x, y) is typically invalid without special support in MyClass. To be able to use that kind of pattern, the class needs to define a __match_args__ attribute.

object.__match_args__

A esta variable de clase se le puede asignar una tupla de cadenas. Cuando esta clase se utiliza en un patrón de clase con argumentos posicionales, cada argumento posicional se convertirá en un argumento de palabra clave, utilizando el valor correspondiente en __match_args__ como palabra clave. La ausencia de este atributo es equivalente a establecerlo en ().

Por ejemplo, si MyClass.__match_args__ es ("left", "center", "right") eso significa que case MyClass(x, y) es equivalente a case MyClass(left=x, center=y). Ten en cuenta que el número de argumentos en el patrón debe ser menor o igual que el número de elementos en __match_args__; si es más grande, el intento de coincidencia de patrón producirá un TypeError.

Nuevo en la versión 3.10.

Ver también

PEP 634 - Coincidencia de patrones estructurales

La especificación para la declaración match de Python.

3.3.11. Búsqueda de método especial

Para clases personalizadas, invocaciones implícitas de métodos especiales solo están garantizados para trabajar correctamente si son definidos en un tipo de objeto, no en el diccionario de instancia del objeto. Ese comportamiento es la razón por la que el siguiente código lanza una excepción:

>>> class C:
...     pass
...
>>> c = C()
>>> c.__len__ = lambda: 5
>>> len(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'C' has no len()

The rationale behind this behaviour lies with a number of special methods such as __hash__() and __repr__() that are implemented by all objects, including type objects. If the implicit lookup of these methods used the conventional lookup process, they would fail when invoked on the type object itself:

>>> 1 .__hash__() == hash(1)
True
>>> int.__hash__() == hash(int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor '__hash__' of 'int' object needs an argument

Intentar invocar de manera incorrecta el método no ligado de una clase de esta forma a veces es denominado como ‘confusión de metaclase’, y se evita sobrepasando la instancia al buscar métodos especiales:

>>> type(1).__hash__(1) == hash(1)
True
>>> type(int).__hash__(int) == hash(int)
True

In addition to bypassing any instance attributes in the interest of correctness, implicit special method lookup generally also bypasses the __getattribute__() method even of the object’s metaclass:

>>> class Meta(type):
...     def __getattribute__(*args):
...         print("Metaclass getattribute invoked")
...         return type.__getattribute__(*args)
...
>>> class C(object, metaclass=Meta):
...     def __len__(self):
...         return 10
...     def __getattribute__(*args):
...         print("Class getattribute invoked")
...         return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__()                 # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c)          # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c)                      # Implicit lookup
10

Bypassing the __getattribute__() machinery in this fashion provides significant scope for speed optimisations within the interpreter, at the cost of some flexibility in the handling of special methods (the special method must be set on the class object itself in order to be consistently invoked by the interpreter).

3.4. Corrutinas

3.4.1. Objetos esperables

An awaitable object generally implements an __await__() method. Coroutine objects returned from async def functions are awaitable.

Nota

The generator iterator objects returned from generators decorated with types.coroutine() are also awaitable, but they do not implement __await__().

object.__await__(self)

Debe retornar un iterator. Debe ser utilizado para implementar objetos awaitable. Por ejemplo, asyncio.Future implementa este método para ser compatible con la expresión await.

Nota

The language doesn’t place any restriction on the type or value of the objects yielded by the iterator returned by __await__, as this is specific to the implementation of the asynchronous execution framework (e.g. asyncio) that will be managing the awaitable object.

Nuevo en la versión 3.5.

Ver también

PEP 492 para información adicional sobre objetos esperables.

3.4.2. Objetos de corrutina

Coroutine objects are awaitable objects. A coroutine’s execution can be controlled by calling __await__() and iterating over the result. When the coroutine has finished executing and returns, the iterator raises StopIteration, and the exception’s value attribute holds the return value. If the coroutine raises an exception, it is propagated by the iterator. Coroutines should not directly raise unhandled StopIteration exceptions.

Las corrutinas también tienen los métodos mencionados a continuación, los cuales son análogos a los de los generadores. (ver Métodos generador-iterador). Sin embargo, a diferencia de los generadores, las corrutinas no soportan directamente iteración.

Distinto en la versión 3.5.2: Es un error RuntimeError esperar a una corrutina más de una vez.

coroutine.send(value)

Starts or resumes execution of the coroutine. If value is None, this is equivalent to advancing the iterator returned by __await__(). If value is not None, this method delegates to the send() method of the iterator that caused the coroutine to suspend. The result (return value, StopIteration, or other exception) is the same as when iterating over the __await__() return value, described above.

coroutine.throw(value)
coroutine.throw(type[, value[, traceback]])

Raises the specified exception in the coroutine. This method delegates to the throw() method of the iterator that caused the coroutine to suspend, if it has such a method. Otherwise, the exception is raised at the suspension point. The result (return value, StopIteration, or other exception) is the same as when iterating over the __await__() return value, described above. If the exception is not caught in the coroutine, it propagates back to the caller.

coroutine.close()

Causa que la corrutina misma se borre a sí misma y termine su ejecución. Si la corrutina es suspendida, este método primero delega a close(), si existe, del iterador que causó la suspensión de la corrutina. Luego lanza una excepción GeneratorExit en el punto de suspensión, causando que la corrutina se borre a sí misma. Finalmente, la corrutina es marcada como completada, aún si nunca inició.

Objetos de corrutina son cerrados automáticamente utilizando el proceso anterior cuando están a punto de ser destruidos.

3.4.3. Iteradores asíncronos

Un iterador asíncrono puede llamar código asíncrono en su método __anext__.

Iteradores asíncronos pueden ser utilizados en la declaración async for.

object.__aiter__(self)

Debe retornar un objeto de iterador asíncrono.

object.__anext__(self)

Debe retornar un esperable (awaitable) resultante en el siguiente valor del iterador. Debe levantar una excepción StopAsyncIteration cuando la iteración termina.

Un ejemplo de objeto iterable asíncrono:

class Reader:
    async def readline(self):
        ...

    def __aiter__(self):
        return self

    async def __anext__(self):
        val = await self.readline()
        if val == b'':
            raise StopAsyncIteration
        return val

Nuevo en la versión 3.5.

Distinto en la versión 3.7: Prior to Python 3.7, __aiter__() could return an awaitable that would resolve to an asynchronous iterator.

Starting with Python 3.7, __aiter__() must return an asynchronous iterator object. Returning anything else will result in a TypeError error.

3.4.4. Gestores de contexto asíncronos

Un gestor de contexto asíncrono es un gestor de contexto que puede suspender la ejecución en sus métodos __aenter__ y __aexit__.

Los gestores de contexto asíncronos pueden ser utilizados en una declaración async with.

object.__aenter__(self)

Semantically similar to __enter__(), the only difference being that it must return an awaitable.

object.__aexit__(self, exc_type, exc_value, traceback)

Semantically similar to __exit__(), the only difference being that it must return an awaitable.

Un ejemplo de una clase de gestor de contexto asíncrono:

class AsyncContextManager:
    async def __aenter__(self):
        await log('entering context')

    async def __aexit__(self, exc_type, exc, tb):
        await log('exiting context')

Nuevo en la versión 3.5.

Notas a pie de página