if instance exists problem ..

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Oct 17 23:00:56 EDT 2007


On Wed, 17 Oct 2007 23:12:15 +0000, Paul Hankin wrote:

> 'if x' doesn't test if x exists, it tests if x when cast to a bool is
> True.

To be pedantic:

Python doesn't have type casts. bool(x) doesn't cast x as a bool, it 
creates a brand new Boolean object from x.

Also, the "if x" test looks at x.__nonzero__() first, and if that method 
doesn't exist, it looks to see if x.__len__() returns 0 or not. See

http://docs.python.org/ref/customization.html


-- 
Steven.



More information about the Python-list mailing list