[Python-checkins] python/dist/src/Lib ftplib.py,1.66,1.67

Neal Norwitz neal@metaslash.com
Thu, 09 May 2002 10:36:40 -0400


Jack Jansen wrote:
> 
> On woensdag, mei 8, 2002, at 06:02 , Guido van Rossum wrote:
> >> And in general I think it's unwise in Python to have an object that
> >> tests false (because it's __len__ returns 0, in the case of this bug
> >> report) but is still expected to do something. "if object:
> >> do_something_with(object)" is fairly standard Python idiom.
> >
> > But it's a time bomb waiting to go off!  The correct code should read
> > "if object is not None: do_something_with(object)".  There are lots of
> > objects that (for better or for worse!) define __len__.  So beware.
> 
> Hmm. I hadn't noticed this (yet), but I wonder whether it's then
> a good idea to let pychecker or so test for this, especially the
> case where an optional argument is used in a simple "if arg:"
> test. I know that I wrote a lot of code like this over the
> years...

There is a feature request for pychecker to do this:
https://sourceforge.net/tracker/index.php?func=detail&aid=530528&group_id=24686&atid=382220

I haven't done anything because I think there would
be too many false positives.  Is there a set of rules
which would indicate when this is dangerous?  Of course,
the rules would also eliminate cases which should be ok.

Neal