Python 2 vs Python 3-vars()的行为差异

时间:2019-07-10 11:37:38

标签: python-3.x python-2.7

vars()在列表理解中使用时,与python 3相比,在python 3中的行为有所不同

Python 3:

In [1]: def hello(test=None): 
   ...:     print(vars()) 
   ...:     print([vars() for i in [0]]) 
   ...:                                                                                                                                                                                                                               

In [2]: hello()                                                                                                                                                                                                                       
{'test': None}
[{'.0': <tuple_iterator object at 0x7fae943e6b38>, 'i': 0}]

Python 2:

In [1]: def hello(test=None): 
   ...:     print(vars()) 
   ...:     print([vars() for i in [0]]) 
   ...: 

In [2]: hello()
{'test': None}
[{'test': None, 'i': 0}]

tuple_iterator对象是一个空列表,没有'test'

0 个答案:

没有答案