2

I want to have a bytes object with the size of 1. I tried the built-in declaration:

>>> bytes(3)
b'\x00\x00\x00'

However, even though set the parameter equal to zero, the size of this exact object is not zero because of its internal structure.

>>> a = bytes(0)
>>> import sys
>>> sys.getsizeof(a)
33

So is there a way to create an object whose size is only 1 byte? Honestly I do not have to have bytes ojects, other data structures are good to my program as well as long as it is only one byte.

Memphis Meng
  • 1,267
  • 2
  • 13
  • 34
  • I don't think you'll get much joy with this problem -- Python objects take more space than the data they hold because there's [an overhead that depends on the implementation.](https://stackoverflow.com/a/14372035/843953) – Pranav Hosangadi Sep 23 '20 at 19:04
  • 1
    Everything is an object in python. Because of that, there will always be some overhead in addition to the data it holds. This overhead will become smaller the more data you store in the object. e.g. a bytes object with 1 byte in it will be 33 bytes, A bytes object with 1000 bytes will be 1033 bytes long. That is only 1.03 bytes length per byte. – Wups Sep 23 '20 at 19:11
  • I see, but is there really no way to create something with only one byte?? – Memphis Meng Sep 23 '20 at 19:11

0 Answers0