Python:为什么存在int.numerator和int.denominator? [英] Python: Why do int.numerator and int.denominator exist?

查看:293
本文介绍了Python:为什么存在int.numerator和int.denominator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int.numeratorint.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆