Are all Python variables actually just items of a dictionary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Are all Python variables actually just items of a dictionary?

vars is a function that can be used to get all variables from a namespace as dictionary in 'variable_name': variable_value pairs. This would have helped me lot in a project, but I thought it would be discouraged from using, so I thought I would manually use a dictionary instead to get the same result: object.dictionary = {'variable_name': variable_value}, and I ended up to do some speed tests using timeit and I found up that using the vars function is often as fast as just mentioning a dictionary in a code: https://code.sololearn.com/cz5iB1Ib7dXh/#py This could mean that all Python variables are just items in a dictionary, but is it really so?

5th Feb 2020, 9:45 AM
Seb TheS
Seb TheS - avatar
1 Answer
+ 8
While I can not state for a fact it is, it does seem reasonable. Python is interpreted so all variables must be stored in a table for that program to access them. Given they have to implement dictionaries for your code, it would make sense to reuse that for theirs.
5th Feb 2020, 10:14 PM
John Wells
John Wells - avatar