classification
Title: vars() dictionary access to generate variables
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, jdan, r.david.murray
Priority: normal Keywords:

Created on 2010-08-04 18:39 by jdan, last changed 2010-08-06 19:51 by r.david.murray. This issue is now closed.

Messages (2)
msg112857 - (view) Author: Dan L (jdan) Date: 2010-08-04 18:39
Perhaps it's assumed that you should know about this by knowing about how the vars dictionary is implemented, but to someone unfamiliar like me it seems like the builtin functions documentation for vars() should mention that you can create a variable name from a string using vars()['string_containing_variable_name'] = value, i.e. 

>>> vars()['hi']=3
>>> hi
3
>>>


Just to include text for a possible fix (to be appended to the existing description):

"You can create a variable name from a string using vars()['string_containing_variable_name'] = value, i.e. 

>>> vars()['hi']=3
>>> hi
3
>>>"
msg113128 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-08-06 19:51
No, the docs are correct as they stand.  They say that without an argument it acts like locals(), and there is even a special note saying you shouldn't modify the returned dictionary because what happens when you do is undefined.  It just so happens that when locals==globals, what you show works, but it is not guaranteed to work even in CPython, and is reasonably likely to not work in other Python interpreters.
History
Date User Action Args
2010-08-06 19:51:38r.david.murraysetstatus: open -> closed

type: behavior

nosy: + r.david.murray
messages: + msg113128
resolution: not a bug
stage: resolved
2010-08-04 18:39:45jdancreate