
reducing memory cost of boolean in python
while scrolling through twitter i came across a tweet about how python boolean variable takes up 192 bits. this was new to me, i had never really thought about how much space python variables actually take(if this was a concern i should not have been using python at all). so i researched it a bit and though it wasn't entirely correct, it wasn't entirely wrong either (explanation below). this prompted me into looking more into how bools are represented in python and how (possibly using underhanded means) can we reduce the space taken by booleans. booleans in python booleans are simple. just 2 values - TRUE or FALSE. theoretically they can be represented by just 1 bit (0 or 1). so why does python use 192 bits (on 32 bit systems) and 224 bits (on 64 bit systems)? the True and False values in cpython are not primitives, but a python object (a subclass of int actually, which is also a python object and not a primitive). the catch however is that the bool class is a singleton class (there ex
Continue reading on Dev.to Python
Opens in a new tab

