为什么不能将“1.7”直接转换为整数,而不先转换为浮点数?
Why is it not possible to convert "1.7" to integer directly, without converting to float first?
当我键入 int("1.7")
Python returns 错误(具体来说,ValueError)。我知道我可以通过 int(float("1.7"))
将它转换为整数。我想知道为什么第一种方法returns错误。
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base ...
显然,"1.7"
不表示基数中的整数文字。
如果您想知道为什么 python 开发人员决定将自己限制为基数中的整数文字,可能有无数种原因,而您' d 必须问 Guido 等人。肯定知道。一种猜测是易于实施+效率。您可能认为他们很容易将其实现为:
- 将数字解释为浮点数
- 截断为整数
不幸的是,这在 python 中不起作用,因为整数可以具有任意精度,而浮点数不能。特殊大小写大数可能导致普通情况下效率低下1.
此外,强制您执行 int(float(...))
的额外好处是清晰度 -- 它使输入字符串 可能 看起来更明显,这有助于在别处调试。事实上,我可能会争辩说,即使 int
会接受像 "1.7"
这样的字符串,最好还是写 int(float("1.7"))
以提高代码的清晰度。
1假设一些验证。其他语言跳过这个——例如ruby
将评估 '1e6'.to_i
并给你 1
因为它在第一个非整数字符处停止解析。似乎这可能会导致有趣的错误来追踪...
python 文档中很好地说明了为什么您不能这样做。
https://docs.python.org/2/library/functions.html#int
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.
基本上从字符串类型转换为整数,字符串不能包含“.”
我们对 "make an int out of this float" 的含义有一个很好的、明显的想法,因为我们将浮点数视为两部分,我们可以丢弃其中的一部分。
当我们有一个字符串时,它就不那么明显了。将这个字符串变成浮点数意味着关于字符串内容的各种微妙的东西,这不是一个正常人希望在值不明显的代码中看到的东西。
所以简短的回答是:Python 喜欢明显的东西,不鼓励魔法。
打破向后兼容性。它 是 当然是可能的,但是这将是一个糟糕的想法,因为它会破坏与非常古老且行之有效的 Python 依赖尝试的习惯用法的向后兼容性。 .except ladder("Easier to ask forgiveness than permission") 判断字符串内容的类型。这个成语至少从 Python 1.5 开始就已经存在并使用了,AFAIK;这里有两个引文:[1] [2]
s = "foo12.7"
#s = "-12.7"
#s = -12
try:
n = int(s) # or else throw an exception if non-integer...
print "Do integer stuff with", n
except ValueError:
try:
f = float(s) # or else throw an exception if non-float...
print "Do float stuff with", f
except ValueError:
print "Handle case for when s is neither float nor integer"
raise # if you want to reraise the exception
还有一件小事:这不仅仅是数字是否包含“.”的问题。科学计数法或任意字母也可能破坏字符串的完整性。
示例:int("6e7")
不是整数(以 10 为底)。然而 int("6e7",16)
=
1767 是一个以 16 为基数(或任何 >=15 的基数)的整数。但是 int("6e-7")
从来都不是一个整数。
(如果将基数扩展为 base-36,任何合法的字母数字字符串(或 Unicode)都可以解释为表示整数,但默认情况下这样做通常是一种糟糕的行为,因为 "dog" 或 "cat" 不太可能是对整数的引用)。
当我键入 int("1.7")
Python returns 错误(具体来说,ValueError)。我知道我可以通过 int(float("1.7"))
将它转换为整数。我想知道为什么第一种方法returns错误。
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base ...
显然,"1.7"
不表示基数中的整数文字。
如果您想知道为什么 python 开发人员决定将自己限制为基数中的整数文字,可能有无数种原因,而您' d 必须问 Guido 等人。肯定知道。一种猜测是易于实施+效率。您可能认为他们很容易将其实现为:
- 将数字解释为浮点数
- 截断为整数
不幸的是,这在 python 中不起作用,因为整数可以具有任意精度,而浮点数不能。特殊大小写大数可能导致普通情况下效率低下1.
此外,强制您执行 int(float(...))
的额外好处是清晰度 -- 它使输入字符串 可能 看起来更明显,这有助于在别处调试。事实上,我可能会争辩说,即使 int
会接受像 "1.7"
这样的字符串,最好还是写 int(float("1.7"))
以提高代码的清晰度。
1假设一些验证。其他语言跳过这个——例如ruby
将评估 '1e6'.to_i
并给你 1
因为它在第一个非整数字符处停止解析。似乎这可能会导致有趣的错误来追踪...
python 文档中很好地说明了为什么您不能这样做。
https://docs.python.org/2/library/functions.html#int
If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.
基本上从字符串类型转换为整数,字符串不能包含“.”
我们对 "make an int out of this float" 的含义有一个很好的、明显的想法,因为我们将浮点数视为两部分,我们可以丢弃其中的一部分。
当我们有一个字符串时,它就不那么明显了。将这个字符串变成浮点数意味着关于字符串内容的各种微妙的东西,这不是一个正常人希望在值不明显的代码中看到的东西。
所以简短的回答是:Python 喜欢明显的东西,不鼓励魔法。
打破向后兼容性。它 是 当然是可能的,但是这将是一个糟糕的想法,因为它会破坏与非常古老且行之有效的 Python 依赖尝试的习惯用法的向后兼容性。 .except ladder("Easier to ask forgiveness than permission") 判断字符串内容的类型。这个成语至少从 Python 1.5 开始就已经存在并使用了,AFAIK;这里有两个引文:[1] [2]
s = "foo12.7"
#s = "-12.7"
#s = -12
try:
n = int(s) # or else throw an exception if non-integer...
print "Do integer stuff with", n
except ValueError:
try:
f = float(s) # or else throw an exception if non-float...
print "Do float stuff with", f
except ValueError:
print "Handle case for when s is neither float nor integer"
raise # if you want to reraise the exception
还有一件小事:这不仅仅是数字是否包含“.”的问题。科学计数法或任意字母也可能破坏字符串的完整性。
示例:int("6e7")
不是整数(以 10 为底)。然而 int("6e7",16)
=
1767 是一个以 16 为基数(或任何 >=15 的基数)的整数。但是 int("6e-7")
从来都不是一个整数。
(如果将基数扩展为 base-36,任何合法的字母数字字符串(或 Unicode)都可以解释为表示整数,但默认情况下这样做通常是一种糟糕的行为,因为 "dog" 或 "cat" 不太可能是对整数的引用)。