bhp
bhp

Reputation: 111

When operating on immutable objects in Python, how is the new object created?

I understand that immutable objects cannot be changed in place. A new object is created and reassigned to the same variable name. In this way, we can maintain the association to the variable. Internally, the variable points to a different object.

>>> string = 'object'
>>> old_id = id(string)
>>> old_id
4452633584
>>> string = 'new' + string
>>> new_id = id(string)
>>> new_id
4501338544
>>> old_id == new_id
False

In the above example, does Python create a mutable clone of string to concatenate to 'new', and then, make this new object immutable before reassignment to string?

I don't understand how objects, that cannot be changed in place, can also be operated on.

Doesn't Python need a changeable thing to change in order to produce a new thing? If I want to hammer a nail into a piece of wood, but that wood is unchangeable, how do I end up with a hammered piece of wood?

Upvotes: 2

Views: 173

Answers (1)

mkrieger1
mkrieger1

Reputation: 23150

Immutability is a concept at the language level. The language specifies that the implementation does not give you a tool to modify a string object once it has been created.

The implementation itself may have internal tools to modify a string while creating it, before it is made accessible to you.

For example, in the CPython string implementation, string concatenation is performed by creating an empty PyUnicode object with the new size, which represents the string internally. Then the characters from the two original strings are copied in, before the object is returned:

PyObject *
PyUnicode_Concat(PyObject *left, PyObject *right)
{
    PyObject *result;
    /* […] */
    Py_ssize_t left_len, right_len, new_len;

    /* […] */

    left_len = PyUnicode_GET_LENGTH(left);
    right_len = PyUnicode_GET_LENGTH(right);

    /* […] */

    result = PyUnicode_New(new_len, maxchar);
    /* […] */
    _PyUnicode_FastCopyCharacters(result, 0, left, 0, left_len);
    _PyUnicode_FastCopyCharacters(result, left_len, right, 0, right_len);
    /* […] */
    return result;
}

If I want to hammer a nail into a piece of wood, but that wood is unchangeable, how do I end up with a hammered piece of wood?

In this analogy, it means that you are given a piece of wood with the nail already in it, but you are not given a hammer. The language implementation has put the nail in the wood for you, but you cannot assume anything about how it did it. It may have used a hammer, or it may have grown a tree around the nail.

Upvotes: 3

Related Questions