Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify int8/int16 in Python

Tags:

python

Int type is int32 or int64 in default in Python3.
How can we specify int8/int16 when declaring variable?
It is not possible in Python?
It is waste of memory if the int object is small and int8/int16 cannot be specified.

like image 734
harry Avatar asked Oct 18 '25 11:10

harry


2 Answers

Python doesn't have any built-in support for 8 or 16-bit integers. NumPy, on the other hand, does support the sizes you're looking for:

import numpy as np
print(np.uint8(22))
like image 196
BrokenBenchmark Avatar answered Oct 21 '25 14:10

BrokenBenchmark


Afaik python chooses the type according to the size of the number and there is no way of specifying which type of int you want python to use. If you are concerned about the waste of memory, python may not be the correct choice in the first place.

like image 44
hjpev Avatar answered Oct 21 '25 15:10

hjpev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!