Title: Array module should support "boolean" natively
Type: enhancement Stage: resolved
Components: Versions: Python 3.4
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: jcea, mark.dickinson, terry.reedy
Priority: normal Keywords: easy

Created on 2013-02-07 15:53 by jcea, last changed 2013-02-11 10:55 by jcea. This issue is now closed.

Messages (6)
msg181623 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2013-02-07 15:53
Array module is used, frequently, as a convenient way of saving a lot of memory. I think we should support "boolean" typeobject natively. Implementation should be trivial and efficient, except methods like "pop()" (a bit convoluted, but doable).

msg181627 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-02-07 16:24
What do you mean by 'natively'?  How much space do you envisage each bool taking?  That is, are you suggesting a packed bool representation with 8 entries to a byte, or storing one bool per byte, or something else?
msg181655 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2013-02-08 03:38
1 byte = 8 bools.
msg181713 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-02-09 01:58
You are proposing a bit array. Whether the bits are interpreted or displayed as 0/1 or f/t or False/True is secondary. The problem is that bit arrays do not fit the array model, with its minimum byte size per element of 1. There are other aspects of arrays that do not fit either. What would .itemsize() return? fractions. Fraction(1,8)? In any case, the internal implementation will be substantially different.

So I suggest that the proposal be to add an array.bitarray class. The bit representation strings '0'/'1', 'f'/'t', 'False'/'True' could be parameters. Omit typecode, typecodes, amd itemsize attributes and byteswap and (to or from)string methods. The buffer_info method would need redefinition. Should (to/from)(bytes/file) use 1 byte per bit (is so, which ones) or pack 8 bits per byte? It would be sensible to add bitwise operators (~, &, |, ^) on bit arrays of the same length. An implementation decision is the granularity of the internal storage (1, 2, 4, or possibly 8 bytes on 64 bit systems -- or just use 3.x ints).  The insert/delete methods might be omitted, but implementation of such should be similar to the shift methods for integers, which have the same problem of moving bits between internal implementation chunks.

I suspect that many have implemented versions of this in Python (as well as other languages) using bytes or ints with masks. I suggest you present the idea on python-ideas list to garner more support -- and be prepred to write a PEP for a new class.

I suspect that the relative ease of using ints as bit arrays will be an argument against such an addition. That is why they have the bit operations. On the other hand, one might argue that the inclusion of bit operations acknowledges the need for bit arrays.
msg181732 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-02-09 11:12
There's already a fairly well known 3rd-party library for this:

I'd be -1 on putting something like this in the standard library:  the array module doesn't get enough maintenance as it is, and a packed bit array sounds like a specialist need.
msg181892 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2013-02-11 10:55
we have a -1, so I close this as "rejected".

I still think it is a valuable idea to pursuit.
Date User Action Args
2013-02-11 10:55:30jceasetstage: resolved
2013-02-11 10:55:17jceasetstatus: open -> closed
resolution: rejected
messages: + msg181892
2013-02-09 11:12:19mark.dickinsonsetmessages: + msg181732
2013-02-09 01:58:18terry.reedysetnosy: + terry.reedy
messages: + msg181713
2013-02-08 03:38:56jceasetmessages: + msg181655
2013-02-07 16:24:24mark.dickinsonsetnosy: + mark.dickinson
messages: + msg181627
2013-02-07 15:53:40jceacreate