The last element (bitstring.BitArray) in list is incorrect after XORing python
Question: I have snippet of code:

    
    
    #!/usr/bin/python3
    
    from bitstring import BitArray
    import itertools
    
    # Helper functions
    
    def get_bitset_by_letter(letter, encoding):
        return encoding[letter] if letter in encoding else None
    
    def get_letter_by_bitset(bitset, encoding):
        for letter, encoding_bitset in encoding.items():
            if bitset == encoding_bitset:
                return letter
    
        return None
    
    class Word():
        def __init__(self, word, encoding):
            self.encoding = encoding
            self.as_word = ''
            self.as_bits = []
    
            if isinstance(word, str):
                self.as_word = word
                self._encode_to_bits()
            elif all(isinstance(i, BitArray) for i in word):
                self.as_bits = word
                self._decode_to_word()
            else:
                raise('class Word: Unexpected type of word arg')
    
        def __ixor__(self, other):
            for index, bitset in enumerate(other.as_bits):
                print('In XOR: %d-%s-%s' % (index, self.as_bits[index], bitset))
                self.as_bits[index] ^= bitset
            return self
    
        def as_bits(self):
            return self.as_bits
    
        def as_word(self):
            return self.as_word
    
        def _encode_to_bits(self):
            for letter in self.as_word:
                bitset = get_bitset_by_letter(letter, self.encoding)
                if bitset is None:
                    raise('Can not find bitset by given letter %s in encoding.' % str(letter))
                else:
                    self.as_bits.append(bitset)
    
        def _decode_to_word(self):
            for bitset in self.as_bits:
                letter = get_letter_by_bitset(bitset, self.encoding)
                if letter is None:
                    raise('Can not find letter by given bitset %s in encoding.' % str(bitset))
                else:
                    self.as_word += letter
    
    def main():
        encoding = {
            'A': BitArray(bin='000'),
            'B': BitArray(bin='001'),
            'C': BitArray(bin='010'),
            'D': BitArray(bin='011'),
            'E': BitArray(bin='100'),
            'F': BitArray(bin='101'),
            'G': BitArray(bin='110'),
            'H': BitArray(bin='111'),
        }
    
        print(encoding)
        print()
    
        word_1 = Word('ABCHE', encoding)
        print('word_1 = %s' % word_1.as_bits)
        word_2 = Word('FGDEA', encoding)
        print('word_2 = %s' % word_2.as_bits)
        word_1 ^= word_2
        print(word_1.as_bits)
    
    
    if __name__ == '__main__':
        main()
    

Details: I use `bitstring.BitArray` class to represent letter as set of three
bits and code words this way (some kind of hometask). Also, my code should be
able to do XOR for this words. Problem: Every last element for `other.as_bits`
list has incorrect value, when it arrives to function. If I comment the line

    
    
    #self.as_bits[index] ^= bitset
    

the last value of the `other.as_bits` is correct. What am I doing wrong?

Here is the output (python 3.5.2):

    
    
    {'C': BitArray('0b010'), 'B': BitArray('0b001'), 'G': BitArray('0b110'), 
    'F': BitArray('0b101'), 'A': BitArray('0b000'), 'H': BitArray('0b111'), 
    'D': BitArray('0b011'), 'E': BitArray('0b100')}
    
    word_1 = [BitArray('0b000'), BitArray('0b001'), BitArray('0b010'), BitArray('0b111'), BitArray('0b100')]
    
    word_2 = [BitArray('0b101'), BitArray('0b110'), BitArray('0b011'), BitArray('0b100'), BitArray('0b000')]
    
    In XOR: 0-0b000-0b101
    
    In XOR: 1-0b001-0b110
    
    In XOR: 2-0b010-0b011
    
    In XOR: 3-0b111-0b100
    
    In XOR: 4-0b100-0b101
    
    [BitArray('0b101'), BitArray('0b111'), BitArray('0b001'), BitArray('0b011'), BitArray('0b001')]
    

From this you can see, that the last 4th element of `word_2` in `__ixor__` is
`0b101`, should be `0b000`

Thank you!


Answer: I solved this using `__xor__` instead of `__ixor__`.

