Chaning self?

M

Miki Tebeka

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top