How are variables names stored and mapped internally?

前端 未结 1 1382
余生分开走
余生分开走 2021-01-01 22:59

I read https://stackoverflow.com/a/19721096/1661745 and it seems that in CPython, variables are simply names that are associated with references.

Th

相关标签:
1条回答
  • 2021-01-01 23:32

    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).

    0 讨论(0)
提交回复
热议问题