【发布时间】:2023-04-10 12:28:01
【问题描述】:
当我在玩 Python 时,
>>> [attr for attr in dir(1) if not attr.startswith('_')]
['bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> [attr for attr in dir(1.1) if not attr.startswith('_')]
['as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
虽然我知道'conjugate'、'imag'和'real'是为了与复杂类型兼容,但我不明白为什么'numerator'和'denominator'只存在于int,而不是不是为了花车。
对此有何解释?
【问题讨论】:
-
您希望
math.pi.denominator返回什么? -
我会说 7,但在 Wikipedia-ing 之后,我了解到 pi 是无理数,并不完全等于有理数的 22/7。
标签: python types floating-point int