question of style

Paul Rubin http
Sat Jul 4 14:10:48 EDT 2009


Simon Forman <sajmikins at gmail.com> writes:
> > if not (self.higher and self.lower):
> >     return self.higher or self.lower
> 
> That would work too in this case, both higher and lower are expected
> to be either None or an object (that doesn't override the default all-
> objects-are-true rule.)

-1.

Objects can support methods like __bool__, __len__ (a tree might use
this to compute the number of nodes or something like that), and
__nonzero__.  If len(object)==0 then the object is treated as false.
So that test can fail if a __len__ method gets added to the tree class
sometime.  Or you might decide you want to support empty trees which
would test as false.

Anyway, Python's overloading of bool(...) is yet another misfeature
and although it's convenient, the "explicit is better than implicit"
principle indicates to avoid that sort of trick.  



More information about the Python-list mailing list