Reputation: 890
I have a variable x
in my code that takes only three values x = {1, 2, 3}
. When use the sys.getsizeof() I get 24
which is the size of an object in bytes.
I was wondering if it's possible in python to convert x
to char with 1 byte
size. I used the str(x)
but sys.getsizeof(str(x))
printed 38 bytes
.
Upvotes: 0
Views: 339
Reputation: 2947
It is not possible for a single byte, since python objects always include the overhead of the Python implementation.
Your use case is only relevant in practice, if you have larger amounts of such values (thousands or millions, e.g. an image). In that case you would use for example the array
or bytearray
objects as containers. Another approach would be using numpy
arrays.
Upvotes: 1