Python3:
string = range(10)
print("{}".format(type(string)))
输出:
class 'range'
我只是好奇这个课程'范围'。 谁能解释一下?
但是在Python2中:
输出:
class 'list'
嗯,这是自我解释
答案 0 :(得分:4)
在Python 2 range(val)
中生成一个列表实例,simply a function。因此type(range(10))
将返回class 'list'
。
在Python 3
中,range
相当于Python 2中的xrange
,它返回一个名为range
的新类的实例。有关Python 2/3之间的更多更改/差异,请参阅PEP 3100
。
答案 1 :(得分:2)