On Oct 17, 8:00 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > 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
To be lazy, you can get away with 'if x:' if you *know* that neither the class nor its superclasses override .__nonzero__ or .__len__, because the instance then defaults to true. Classes that *do* override these methods are generally emulating a numeric or collection type, or are using False to signal "something isn't there". ConfigParser is doing the latter. That may or may not have been a good choice by the ConfigParser developers, because it's debatable how useful it is for empty ConfigParsers to be False. But the fact remains that it is this way, and an instance of *any* third-party class may be false for reasons you don't expect. --Mike -- http://mail.python.org/mailman/listinfo/python-list