views:

27

answers:

1

I want to create a namedtuple which represents the individual flags in a short bitfield. I'm trying to subclass it so that I can unpack the bitfield before the tuple is created. However, my current attempt isn't working:

class Status(collections.namedtuple("Status", "started checking start_after_check checked error paused queued loaded")):
    __slots__ = ()

    def __new__(cls, status):
        super(cls).__new__(cls, status & 1, status & 2, status & 4, status & 8, status & 16, status & 32, status & 64, status & 128)

Now, my experience with super() is limited and my experience with __new__ is virtually non-existent, so I'm not quite sure what to make of the (to me) enigmatic error TypeError: super.__new__(Status): Status is not a subtype of super. Googling and digging into the docs haven't yielded anything enlightening.

Help?

+2  A: 

I'd avoid super unless you're explicitly catering to multiple inheritance (hopefully not the case here;-). Just do something like...:

def __new__(cls, status):
    return cls.__bases__[0].__new__(cls,
                                    status & 1, status & 2, status & 4,
                                    status & 8, status & 16, status & 32,
                                    status & 64, status & 128)
Alex Martelli
Worked like a charm. Thanks!
Ben Blank
@Ben, you're welcome!
Alex Martelli