Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Chaning self?

Reply
Thread Tools

Chaning self?

 
 
Miki Tebeka
Guest
Posts: n/a
 
      01-08-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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-08-2004
Subclassing won't work here, because the long type is immutable

Peter


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Q]chaning file permission andrew Cisco 0 01-04-2006 07:25 AM
Chaning mirror+focusing screen - how much would it cost? Marek M. \rogus\ Digital Photography 2 06-03-2005 05:35 PM
asp.net and chaning betten http and https Darren Clark ASP .Net 1 08-20-2004 10:58 AM
Chaning/allocating char* function parameters Johnathan Doe C Programming 5 05-24-2004 10:37 AM
Chaning session state programatically Maximus ASP .Net 0 02-05-2004 06:00 PM



Advertisments