'What is _PyObject_GetAttrId() in CPython?

I looked at sysmodule.c in CPython and came across this codestdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);, but I couldn't find any documentation of it.

What is _PyObject_GetAttrId()?

https://github.com/python/cpython/blob/master/Python/sysmodule.c#L597



Solution 1:[1]

The ...Id family of functions are internal-only CPython optimizations. Instead of allocating a PyObject* string for the identifier name, they have an intern pool of identifiers that they can use.

You can see in the implementation that they can pull out a lazily allocated str with _PyUnicode_FromId.

Solution 2:[2]

My assumption is it's the implementation of the python __getattr__ magic method.

Take a look at this list of magic methods for more information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tekknolagi
Solution 2 Tushar Sadhwani