首页 > 解决方案 > python3, in help(int) text, what is the Data descriptors described?

问题描述

When you get the help string for int types using help(int). The very last part is:

| ----------------------------------------------------------------------
|  Data descriptors defined here:
|  
|  denominator
|      the denominator of a rational number in lowest terms
|  
|  imag
|      the imaginary part of a complex number
|  
|  numerator
|      the numerator of a rational number in lowest terms
|  
|  real
|      the real part of a complex number

So these are attributes of the complex type and fraction class, so why are they listed here in relation to ints. Are the some type of global data descriptor?

标签: pythonintcode-documentation

解决方案


complex并具有具有这些名称和含义的属性这一事实fractions.Fraction并不意味着整数也没有此类属性。不同的类可以随意拥有相似的属性:

>>> (5).denominator
1
>>> (5).imag
0
>>> (5).numerator
5
>>> (5).real
5

它们不是某种通用属性或任何东西。该int类型仅实现这些属性的描述符,以便与其他数字类型进行互操作。具体来说,它们的实施符合PEP 3141


推荐阅读