[英]Python automatically converts the numerator and denominator of some fractions into big numbers. Why?
[英]Python: Why do int.numerator and int.denominator exist?
int.numerator
和int.denominator
对我来说是一个谜。
help(int.numerator)
指出:
最低项中有理数的分子
但据我所知, int
不是一个有理数。 那么为什么这些属性存在呢?
请参阅http://docs.python.org/library/numbers.html - int( numbers.Integral
)是数字的子类型numbers.Rational
。
>>> import numbers
>>> isinstance(1337, numbers.Integral)
True
>>> isinstance(1337, numbers.Rational)
True
>>> issubclass(numbers.Integral, numbers.Rational)
True
int的分母总是1
而其分子是值本身。
在PEP 3141中,您可以找到有关各种数字类型实现的详细信息,例如证明以前的声明:
@property
def numerator(self):
"""Integers are their own numerators."""
return +self
@property
def denominator(self):
"""Integers have a denominator of 1."""
return 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.