Modify int in place

0 votes

This is more of a curiosity

Say I have the following code

>>> my_immutable = (1, 2)
>>> my_immutable[0] += 1

TypeError: 'tuple' object does not support item assignment

This is expected, because unlike C, Python does not modify the underlying int, but rather creates a new one (observed in the code below)

>>> x = 1
>>> id(x)
33156200
>>> x += 1
>>> id(x)
33156176

If I want to modify the underlying integer in the tuple, I can hackly do something like

>>> hacked_immutable = ([1], [2])
>>> hacked_immutable[0][0] += 1
>>> hacked_immutable
([2], [2])

My question is: is there a nicer way of doing it (ideally performant and ideally already in the standard library)? Some wrapper class around int maybe?

Edit: I did not have a specific software that had to adhere to this. It was more of a thought exercise of why are things like this. I think the three questions I had were:

  • Why are ints immutable? (still not sure)

  • Is there a way to force them to be mutable? (wim's answer)

  • Is there a nice way to force them to be mutable (like Integer vs int in Java) - I think the answer is NO?

Thanks a lot for the discussion!

Sep 14, 2018 in Python by bug_seeker
• 15,510 points
1,386 views

1 answer to this question.

0 votes

Use a list rather than a tuple:

my_container = [1, 2]

tuple is immutable - you can't modify them.

int is immutable - you can't modify them.

The closest you can get is using ctypes to monkeypatch the value. But this is not "nice" by any stretch of the imagination and you will probably segfault your Python runtime if anything else happens to be using that integer.

>>> t = (42, 43)
>>> import ctypes
>>> ctypes.cast(id(42), ctypes.POINTER(ctypes.c_int))[6] = 666
>>> t
(666, 43)
answered Sep 14, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

How to round a floating point number up to certain decimal place in Python?

This is normal (and has nothing to do ...READ MORE

answered Oct 8, 2018 in Python by charlie_brown
• 7,720 points
2,371 views
+2 votes
10 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

The following are totally acceptable in python: passing ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,560 points
409,319 views
0 votes
1 answer

Sort a part of a list in place

You can write it as such: a[i:j] = ...READ MORE

answered Feb 12, 2019 in Python by SDeb
• 13,300 points
3,616 views
0 votes
1 answer

KeyError: "['Place'] not found in axis"

Hi@akhtar, You need to provide the axis parameter ...READ MORE

answered Jun 24, 2020 in Python by MD
• 95,460 points
40,811 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,454 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,108 views
0 votes
1 answer

Modify int in place

Use a list rather than a tuple: my_container ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,020 points
567 views
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 58,020 points
6,483 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP