how to use bool

Fredrik Lundh fredrik at
Thu Jan 3 16:56:45 CET 2008

jimgardener at wrote:

> hi, i have some code where i set a bool type variable and if the value
> is false i would like to return from the method with an error msg..
> being a beginner I wd like some help here
> class myclass:
>      .........
>     def  mymethod(self):
>              success=True
>              msg="all validation OK"
>              success=validateSthing()
>              if(success==False):
>                    msg="sthing failed"
>                    return (success,msg)
>              dosomeprocessing()
>              .....
>              success=validateSthingelse()
>              if(success==False):
>                    msg="sthingelse  failed"
>                    return (success,msg)
>              domoreprocessing()
>               ....
>                return(success,msg)
> i would like to know if this way of doing this is OK..I have need of
> many kinds of validations in this there a better way of doing
> this ?

to test boolean values, it's usually better to use plain "if" or "if 
not" statements:

     if success:
         ... handle success here ...

     if not success:
         ... handle failure here ...

to report failures, use exceptions (the raise and try/except 
statements).  see the tutorial for more on this topic.


More information about the Python-list mailing list