TypeError: 'int' object is not subscriptable

Why integer object cannot be indexable
if Python give this feature would be more intresting

int(‘123’,base=10)[0] =3
int(‘123’,base=10)[1] =20
int(‘123’,base=10)[2] =100
int(‘123’,base=10)[0:2] =23
int(‘123’,base=10)[0:3] =123

int(‘123’,base=10)[-1] =100 ( No Logic !!!)
int(‘123’,base=10)[-2] =20 ( No Logic !!!)

int(‘01110011’, base=2)[0] =1
int(‘01110011’, base=2)[1] =2
int(‘01110011’, base=2)[2]=0
int(‘01110011’, base=2)[3]=0
int(‘01110011’, base=2)[4]=16
int(‘01110011’, base=2)[5]=32
int(‘01110011’, base=2)[6]=64
int(‘01110011’, base=2)[7]=0
int(‘01110011’, base=2)[0:8:2] =81

int(‘FACE’, base=16)[0] =14
int(‘FACE’, base=16)[1] =192
int(‘FACE’, base=16)[2] =2560
int(‘FACE’, base=16)[3] =61440

int(‘1234567’, base=8)[0] =7
int(‘1234567’, base=8)[1] =48
int(‘1234567’, base=8)[2] =320
int(‘1234567’, base=8)[3] =2048
int(‘1234567’, base=8)[4] =12288
int(‘1234567’, base=8)[5] =65536
int(‘1234567’, base=8)[6] =262144

An integer would have to remember the base it was created from. What if there is no base? What would 123[0] return?

You say this as if there’s a difference between int('123', base=10) and int('01110011', base=2) - but there isn’t. Both of them give back the integer 123. At that point, there’s no distinguishing between them.

You could design a “string of digits” class that behaves in this way, but it would be much more string-like than int-like.

1 Like

Aside from the misunderstanding about int this would return 1 not 2
As that is the bit in the int at index 1.

If you want to extract single bits then you can use a mask.
For example get the 4th bit:

print(123&(1<<3))
1 Like

Mr Eric, Thanks for your suggestion

IT IS ASSUMED 10 BASE IF NOT SPECIFIED
SO 123[0] AUTOMATICALLY WOULD YIELD 3

Because your proposal has limited use cases, can be done by other means, and would mean that int objects had to increase in size, I’m afraid there’s realistically no way it would be accepted.