Checking against NULL will be eliminated?

Carl Banks pavlovevidence at gmail.com
Wed Mar 2 09:37:13 EST 2011


On Mar 2, 5:51 am, Claudiu Popa <cp... at bitdefender.com> wrote:
> Hello Python-list,
>
> I  don't  know how to call it, but the following Python 3.2 code seems to raise a
> FutureWarning.
>
> def func(root=None):
>     nonlocal arg
>     if root:
>        arg += 1
> The  warning is "FutureWarning: The behavior of this method will change
> in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead."
> Why is the reason for this idiom to be changed?

I'm guessing root is an ElementTree Element?

The reason for this is that some element tree functions will return
None if an element is not found, but an empty element will have a
boolean value of false because it acts like a container.  Some people
who use ElementTree don't always have this behavior in mind when they
are testing to see if an element was found, and will use "if element:"
when they need to be using "if element is not None:".

The larger reason is that boolean evaluation in Python tries to be too
many things: for some types is means "not zero", for some types it
means "empty", and for some types it means "this is a value of this
type as opposed to None".  That causes conflicts when more than one of
those tests makes sense for a given type, as it does with Elements.

This change is only for ElementTree as far as I know.  (Incidentally,
Numpy arrays are another notable type that's disabled implicit
booleans, but it did it a long time ago.)


Carl Banks



More information about the Python-list mailing list