Chaning self?
Miki Tebeka
miki.tebeka at zoran.com
Thu Jan 8 07:57:57 EST 2004
Hello,
I'm trying to create a class which is a bit array. I've done the following:
class bitarray(long):
def __init__(self, value):
super(long, self).__init__(value)
def __getitem__(self, index):
if self & (1 << index):
return 1
else:
return 0
def __setitem__(self, index, value):
if value not in (0, 1):
raise ValueError("must be 0 or 1")
if value:
self |= (1 << index)
else:
self &= (~ (1 << index))
However the __setitem__ does not work:
>>> b = bitarray(5)
>>> b[0]
1
>>> b[1]
0
>>> b[2]
1
>>> b[0] = 0
>>> b
5L
>>> b[0]
1
What am I missing?
Thanks.
Miki
More information about the Python-list
mailing list