Python:为什么存在int.numerator和int.denominator? [英] Python: Why do int.numerator and int.denominator exist?
问题描述
int.numerator
和int.denominator
对我来说是个谜.
help(int.numerator)
状态:
最低数的有理数分子
the numerator of a rational number in lowest terms
但是据我所知,int
不是一个有理数.那么为什么存在这些属性?
But as far as I know, int
is not a rational number. So why do these properties exist?
推荐答案
请参见 http://docs .python.org/library/numbers.html -int(numbers.Integral
)是numbers.Rational
的子类型.
See http://docs.python.org/library/numbers.html - int (numbers.Integral
) is a subtype of numbers.Rational
.
>>> import numbers
>>> isinstance(1337, numbers.Integral)
True
>>> isinstance(1337, numbers.Rational)
True
>>> issubclass(numbers.Integral, numbers.Rational)
True
int的分母始终为1
,而其分子为值本身.
The denominator of an int is always 1
while its numerator is the value itself.
在> PEP 3141 中,您可以找到详细信息关于各种数字类型的实现,例如证明前面的陈述:
In PEP 3141 you find details about the implementation of the various number types, e.g. proving the previous statement:
@property
def numerator(self):
"""Integers are their own numerators."""
return +self
@property
def denominator(self):
"""Integers have a denominator of 1."""
return 1
这篇关于Python:为什么存在int.numerator和int.denominator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!