Chaning self?

Discussion in 'Python' started by Miki Tebeka, Jan 8, 2004.

  1. Miki Tebeka

    Miki Tebeka Guest

    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
     
    Miki Tebeka, Jan 8, 2004
    #1
    1. Advertising

  2. Miki Tebeka

    Peter Otten Guest

    Subclassing won't work here, because the long type is immutable :-(

    Peter
     
    Peter Otten, Jan 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    625
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    626
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    432
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    406
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    426
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page