我在读《开始Python》这本书。其中提到:
In general, you should not modify the dictionary returned by vars because, according to the official Python documentation, the result is undefined. In other words, you might not get the result you’re after.
我很困惑。你知道吗
As vars()
获取指定对象的__dict__
。
该范围内的所有变量都基于__dict__
。当修改它时,它怎么会被称为undefined
?你知道吗
我这样做了:
>>> x = 0
>>> vars()['x'] += 2
>>> x
>>> 2
在有人改变x的值之前,这个x会保持2的值在那个范围内吗?你知道吗
那么,这里的result is undefined
是什么意思?你知道吗
也许documentation确实回答了你的问题:
作为the documentation states:
切换到
locals
,然后:请注意,“may not”;undefined确切地说,对于应该发生的事情没有确切的定义。换言之,它不能保证,您不应该编写依赖它的代码(无论哪种方式)。你知道吗
相关问题 更多 >
编程相关推荐