Hani Gotc
Hani Gotc

Reputation: 890

Is it possible to convert a digit integer in python into a 1 byte character

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.


Question

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

Answers (1)

code_onkel
code_onkel

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

Related Questions