我使用 Squish 6.4.2 来测试我的 Qt 应用程序,使用 Python 2.7.10 作为测试脚本语言,并在尝试使用 range()
函数时收到以下错误消息:
类型错误:range() 需要整数结束参数,已得到 int。
这是一个小代码示例和我在 Squish 中的输出:
def main():
test.log(str(range(4)))
test.log(str(range(int(4))))
[0, 1, 2, 3]
Error: Script Error
Detail: TypeError: range() integer end argument expected, got int.
如您所见,range(4)
有效,但 range(int(4))
无效。
我尝试在 Squish 应用程序附带的 Python 版本的 Python 控制台中运行相同的代码,并且两个 range()
调用都有效。
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> range(4)
[0, 1, 2, 3]
>>> range(int(4))
[0, 1, 2, 3]
>>>
此外,当我将整数与强制转换的 int 进行比较时,Squish 中的比较结果是错误的。例如在 Squish 中:
def main():
test.log(str(7<4))
test.log(str(7 < int(4)))
给出
False
True
在 Python 控制台中,结果是正确的。
最佳答案
Squish int 与 Python int 不同:
Squish's int represents the Squish type for an integer in the wrapper's C and C++ code.
Like the other hidden symbols, to access Python's int() function one can use it from the package __builtin__ (Python 2) or builtins (Python 3).
(其他类型也不同)
参见Squish's int vs. Python's int
或者更一般地说Python Notes在 Squish 手册中。
关于python - 类型错误 : range() integer end argument expected, 已获取 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55372342/