Python3:
string = range(10)
print("{}".format(type(string)))
输出:
class 'range'
我只是对这个类“范围”感到好奇。 谁能解释一下?
但是在 Python2 中:
输出:
class 'list'
好吧,这是不言自明的
最佳答案
在 Python 2 中,range(val)
生成一个列表实例,它 simply a function .因此 type(range(10))
将返回 class 'list'
。
在 Python 3
中,range
相当于xrange
在 Python 2 中,它返回一个名为 range
的新类的实例。有关 Python 2/3 之间的更多更改/差异,请参阅 PEP 3100
.
关于python - 什么是 <class 'range' >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39241877/