¿Dónde se almacena el valor int de una instancia int en Python?
losint
escribir en Python ofrece dos atributos llamadosnumerator
yreal
que tienen el mismo contenido que__int__()
.
Como todos estos 3 valores devuelven el mismo atributo interno, supongoreal
es una propiedad como:
@property
def real(self):
return self.__int
Sin embargo, no puedo encontrar esta propiedad oculta dirdir
o cualquieraa = int(); a._int__<tab>
en IPython.
Así que miré elcódigo fuente y encontré esto:
static PyGetSetDef int_getset[] = {
{"real",
(getter)int_int, (setter)NULL,
"the real part of a complex number",
NULL},
{"imag",
(getter)int_get0, (setter)NULL,
"the imaginary part of a complex number",
NULL},
{"numerator",
(getter)int_int, (setter)NULL,
"the numerator of a rational number in lowest terms",
NULL},
{"denominator",
(getter)int_get1, (setter)NULL,
"the denominator of a rational number in lowest terms",
NULL},
{NULL} /* Sentinel */
};
Y esto:
static PyObject *
int_int(PyIntObject *v)
{
if (PyInt_CheckExact(v))
Py_INCREF(v);
else
v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
return (PyObject *)v;
}
Pero esto es lo más lejos que puedo llegar solo.
¿Dónde se almacena el valor real de un entero dentro de una instancia de entero?
La razón principal de esta pregunta es que quiero extender elfloat
escribir con unMyFloat
donde me gustaría referirme al valor de la instancia.