[Tutor] evaluating AND

Eric Brunson brunson at brunson.com
Fri Sep 14 00:02:24 CEST 2007


The first is how I would code it.  Python guarantees that compound 
boolean statements are processed from left to right and also that the 
AND operator will "short circuit" the rest of the evaluation, since the 
rest of the line cannot change the falseness of the entire statement.

Orest Kozyar wrote:
> Given a variable x that can either be None or a tuple of two floats [i.e.
> (0.32, 4.2)], which syntax is considered most appropriate under Python
> coding standards?
>
> if x and x[0] > 0:
> 	pass
>
> =====OR=====
>
> if x:
> 	if x[0] > 0:
> 		pass
>
>
> In the first, I'm obviously making the assumption that if the first
> condition evaluates to false, then the second condition won't be evaluated.
> But, is this a good/valid assumption to make?  Is there a more appropriate
> design pattern in Python?
>
> Thanks!
> Orest
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list