[Tutor] am I missing another simpler structure?

Gregor Lingl glingl at aon.at
Thu Dec 16 14:57:32 CET 2004



Kent Johnson schrieb:
> It's probably worth pointing out that these two functions are not 
> entirely equivalent:
> def t1():
>   if condition:
>     return True
>   return False
> 
> def t2():
>   return condition

...

>  >>> if t1(100) == True: print '100 is True'
> ...
> 100 is True
> 
>  >>> if t2(100) == True: print '100 is True'
> ...
> (nothing prints)
> 

If you really need this property, you may use
type-conversion with bool:

 >>> t2(100)
100
 >>> bool(t2(100))
True
 >>>

So in this case

def t3(a):
     return bool(a)

should work correctly

Regards,
Gregor

Remark for Brian: Please note, that Kent wrote about a feature
specific to Python, whereas the main part of this thread
contains considerations concerning boolean expressions in general.


More information about the Tutor mailing list