-Even if I understand much of this article https://python-reference.readthedocs.io/en/latest/docs/functions/intern.html , basically what happens when strings are 'internalized' is that (so I understand) are stored in a place called 'internal string table', considering this, the question is as follows:
if (lo que dije es cierto):
¿Que es eso de tabla de cadenas internas?
else:
¿Entonces que hace la funcion sys.intern()?
(:-|
The documentation is pretty clear.
To directly answer your question:
It is a collection of strings handled behind the scenes by python. In this collection, all strings exist only once, and when you call on
intern()
a string that already exists, instead of creating a new string, it returns the reference to the existing string.We can imagine that this is implemented as an alphabetically sorted list, so that determining if a string exists to insert a new record can be done very quickly.
On the other hand, python will internalize strings that are short (or not so short) automatically.
In the documentation
sys.intern
we can read:You can use the function
id()
to get the memory address of a variable, so you can see how this works with something like:When I test with python 3.9, I get this output:
As you can see, all strings have been interned (even the ones without an explicit call to
intern()
).The main advantage of this is that when doing an equality comparison, such as
if (a==b)
, instead of having to compare character by character, you simply compare the memory address of both variables (a pointer comparison), which results in a more efficient execution (although the difference in time is imperceptible on modern CPU's).In earlier versions of python, the variables
c
andd
could reside at different memory addresses, so an equality comparison will force a character-by-character comparison.