range
Python2和Python3中的range有什么区别?
[root@bj-vmware-test1 tmp]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> lst = range(1, 1000000)
>>> sys.getsizeof(lst)
8000064
>>>
[root@bj-vmware-test1 tmp]# python3
Python 3.4.5 (default, Oct 14 2016, 23:28:20)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> lst = range(1, 1000000)
>>> sys.getsizeof(lst)
48
在python2中range会返回一个列表,而在python三中返回类似生成器一样的对象,它只有用时才会产生数据,Python3这样做对内存很友好