作者:粉红色头发丫头_960 | 来源:互联网 | 2023-05-17 22:22
Theinttypeinpythonofferstwoattributesnamednumeratorandrealthathavethesamecontentas
The int
type in python offers two attributes named numerator
and real
that have the same content as __int__()
.
python中的int类型提供了两个名为numerator和real的属性,它们与__int __()具有相同的内容。
As all of these 3 values returns the same internal attribute, I guess real
is a property like:
由于所有这三个值都返回相同的内部属性,我猜真实是一个属性,如:
@property
def real(self):
return self.__int
However I cannot find this hidden property dir dir
or either a = int(); a._int__
in IPython.
但是我找不到这个隐藏属性dir dir或a = int(); IPython中的a._int __
。
So I looked at the source code and found this:
所以我查看了源代码,发现了这个:
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 */
};
And this:
static PyObject *
int_int(PyIntObject *v)
{
if (PyInt_CheckExact(v))
Py_INCREF(v);
else
v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
return (PyObject *)v;
}
But this the furthest I can go by myself.
但这是我能自己走的最远的。
Where the actual value of an integer is stored inside an integer instance?
整数的实际值存储在整数实例中的位置?
The main reason for this question is that I want to extend the float
type with a MyFloat
where I would like to refer to the value of the instance.
这个问题的主要原因是我想用MyFloat扩展float类型,我想在这里引用实例的值。
1 个解决方案