# Mutating immutable objects with ctypes

**URL:** <https://discuss.python.org/t/mutating-immutable-objects-with-ctypes/43724>\
**Category:** Python Help\
**Created:** [January 20, 2024, 2:41pm UTC](https://discuss.python.org/t/mutating-immutable-objects-with-ctypes/43724 "2024-01-20T14:41:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [January 20, 2024, 2:41pm UTC](https://discuss.python.org/t/mutating-immutable-objects-with-ctypes/43724/1 "2024-01-20T14:41:56Z")

</div>

> [@Is this meant to be an expected behavior?](https://discuss.python.org/t/is-this-meant-to-be-an-expected-behavior/43679/5):
>
> there is no way from within Python to modify the integer object itself

[Actually…](https://stackoverflow.com/q/70882092)

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [January 20, 2024, 2:43pm UTC](https://discuss.python.org/t/mutating-immutable-objects-with-ctypes/43724/2 "2024-01-20T14:43:57Z")

</div>

I wouldn’t call manipulating memory via `ctypes` “within Python”, but I can certainly see the argument to the contrary.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [January 20, 2024, 2:51pm UTC](https://discuss.python.org/t/mutating-immutable-objects-with-ctypes/43724/3 "2024-01-20T14:51:20Z")

</div>

It’s using Python code and not calling on any external code other than the standard library!

That said, though, changing the value of an integer is NOT what we would call, uhh, normal behaviour. If you change the value of `0` to be `42` and the interpreter crashes in approximately three nanoseconds, you can’t exactly complain about a bug.

(Trust me, a LOT of things get confused if zero becomes truthy.)

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [January 20, 2024, 8:38pm UTC](https://discuss.python.org/t/mutating-immutable-objects-with-ctypes/43724/4 "2024-01-20T20:38:28Z")

</div>

> [@Is this meant to be an expected behavior?](https://discuss.python.org/t/is-this-meant-to-be-an-expected-behavior/43679/5):
>
> > [@Is this meant to be an expected behavior?](https://discuss.python.org/t/is-this-meant-to-be-an-expected-behavior/43679/5):
> >
> > there is no way from within Python to modify the integer object itself
> 
> [Actually…](https://stackoverflow.com/q/70882092)

To me, this ‘gotcha’ comment is not only a distraction from the subject of the thread, but is wrong. @kknechtel said “within Python”. The SO answer uses the CPython implementation’s interface to foreign _non-Python_ functions. So “within CPython with cytpes or similar non-Python function access allowed, int object values can be mutated”. Karl, please feel free to write sensibly, as you did, without adding a caveat to every comment.

Most every statement in the _Python_ docs _inplicitly_ assumes that one has not called an implementation-specific foreign function that contradicts the statement. The docs effectively describe Python behavior on a pure Python interpreter that does not have such possibilities. Github python/cpython Issues that report “by using ctypes I made CPython misbehave” get rejected and closed.
