How are variables names stored and mapped internally?

邮差的信 提交于 2019-11-30 18:19:49

I think at a high level it can be done with a dict, where the key is the variable name (str?) and the value is the reference that it's associated with.

This is how it works internally too. In CPython, variable names and the objects they point to are typically stored in a Python dictionary; the very same data structure that you can use when writing Python code.

When you write x = 5, the name x is set as a key in the dictionary of global names with 5 as the corresponding value. You can return and inspect this dictionary using the globals() function, which gives the contents of the current scope's namespace.

So you're also correct that the name x takes up space. It exists as a string somewhere in memory and Python keeps a reference to it for the key of the dictionary.

If you want to peer deeper into the CPython source to see where x gets assigned to the value 5, you could have a look at ceval.c. Writing x = 5 triggers the LOAD_CONST opcode (to put the integer 5 onto the stack) and also the STORE_GLOBAL opcode* (to set the name x as a key in the dictionary with 5 as the value).

Here is the code for the STORE_GLOBAL opcode:

TARGET(STORE_GLOBAL) {
    PyObject *name = GETITEM(names, oparg);
    PyObject *v = POP();
    int err;
    err = PyDict_SetItem(f->f_globals, name, v);
    Py_DECREF(v);
    if (err != 0)
        goto error;
    DISPATCH();
}

You can see the call to PyDict_SetItem to update the globals dictionary.


* If you inspect the bytecode generated by x = 5 (e.g. using dis) you might see the STORE_NAME opcode used. This opcode functions in the same way (see here for a brief description).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!