Some basic questions

Carl Banks imbosol-1048717795 at aerojockey.com
Wed Mar 26 17:44:26 EST 2003


Brian Christopher Robinson wrote:
> Is there any way to write a boolean function?  I wanted to write a simple 
> is_alpha function (which may be in the standard library but I'm leanring), 
> and this is what I came up with:
> 
> def is_alpha(c) :
>    letters = re.compile("[a-zA-Z]")
>    return letters.match(c)
> 
> Instead of checking for true or false I check to see if None is returned, 
> but that seems a little kludgy.

The truth function from the operator module will do what you are
asking for.  Begining with Python 2.3, you can also use the bool
builtin.

Note that this is all unnecessary.  The function will work as
advertised without any such function, and without explicitly testing
against None.  This will work:

    if is_alpha(c):
        print "c is a letter!"
    else:
        print "c is not a letter."


-- 
CARL BANKS




More information about the Python-list mailing list