WebSphere Jython scripting: bool, True and False

I’m always forgetting that Jython 2.1 doesn’t have a bool type.  It’s so easy to forget and use True or False.  It’s a small thing, but so easy to implement we might as well add it to the collection.  Here you go, a bool type and True and False for Jython 2.1:

try:
    True and False
except NameError:
    class bool(type(1)):
        def __init__(self, val = 0):
            if val:
                type(1).__init__(self, 1)
            else:
                type(1).__init__(self, 0)
        def __repr__(self):
            if self:
                return "True"
            else:
                return "False"

        __str__ = __repr__

    __builtin__.bool = bool
    __builtin__.False = bool(0)
    __builtin__.True = bool(1)

Leave a Reply

Your email address will not be published. Required fields are marked *