[Tutor] results: why different?

Daniel Ehrenberg littledanehren at yahoo.com
Thu Jan 15 09:03:46 EST 2004


ali wrote:
> 
> why are the results of these codes different?
> 
> ###### 1 ######
> 
> if True==[1,2,3]:
>     print "test"
> 
> ###### 2 ######
> 
> if [1,2,3]:
>     print "test"
> 
> thanks...
> 
> ali

Think of the if keyword like it's converting its
arguments into a boolean and *then* testing to see if
they equal True. == doesn't do this. Obviously, [1, 2,
3] is not the same thing as True, but converted to a
boolean, [1, 2, 3] is true. Here are some examples to
show this more clearly:

>>> bool([1, 2, 3])
True
>>> [1, 2, 3] == True
False
>>> bool([1, 2, 3]) == True
True
>>> if [1, 2, 3]: print 'yes'
yes
>>> if bool([1, 2, 3]): print 'yes'
yes
>>> if [1, 2, 3] == True: print 'yes'
>>> if bool([1, 2, 3]) == True: print 'yes'
yes

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes
http://hotjobs.sweepstakes.yahoo.com/signingbonus



More information about the Tutor mailing list