Suggestions for good programming practices?
quinn at upchuck.ugcs.caltech.edu
Thu Jun 27 01:53:29 EDT 2002
On Mon, 24 Jun 2002 16:31:14 -0500, Mark McEahern <marklists at mceahern.com>
>How do you choose between these:
> if x is None:
> if x:
>The latter is what you'd use if you wanted to work with instances that
>defined __nonzero__ and or __len__. In other words, you don't really care
>what the type of x is, nor whether it's identity is zero, just whether it
>considers itself nonzero.
>Is one more or less polymorphic than the other? I don't really know. But
>it sure does sound good.
They both serve their own purposes and you have to know what you want.
For example, I recently tracked down an annoying bug where I said 'if x' when
I meant 'if x is not None'. Everything worked until x's __len__ returned 0
and it was hard for me to see 'if x' as being buggy code. I wasted a lot of
time trying to figure out why 'x' was occaisionally None.
More information about the Python-list