[BangPypers] ElementTree nodes

Sidharth Kuruvila sidharth.kuruvila at gmail.com
Sun Oct 25 21:23:58 CET 2009


Hi,
To answer myself. It turns out any object that returns len(o) as 0
will evaluate as false.

class c:
    def __len__(self):
        return 0

i = c()

print bool(i)
# Prints False

On the one side this does make things quite consistent. On the other
it is a bit unintuitive. Any idea if this behaviour holds in python 3?

Regards,
Sidharth


On Mon, Oct 26, 2009 at 1:43 AM, Sidharth Kuruvila
<sidharth.kuruvila at gmail.com> wrote:
> Hi,
>
>  I don't have elementree, so I just wanted to confirm what is
> happening here. Is it that the Element type is a subclass of list.
> Which would lead an empty Element to have the same boolean property as
> an empty list.
>
> Or is there some way of specifying the truthfulness of an object.
>
> Regards,
> Sidharth
>
> On Mon, Oct 26, 2009 at 1:07 AM, Noufal Ibrahim <noufal at gmail.com> wrote:
>> On Mon, Oct 26, 2009 at 12:52 AM, bhaskar jain
>> <bhaskar.jain2002 at gmail.com> wrote:
>>> On Mon, Oct 26, 2009 at 12:31 AM, Noufal Ibrahim <noufal at gmail.com> wrote:
>>>>>>Can you try this with an element that has zero children? From effbot's
>>>>>>docs, I think that's the difference.
>>> "The boolean interpretation will most likely change in future versions, so
>>> that all elements evaluate to true, also if they have no children."
>>> Correct.[..]
>>
>> Yup. Felt weird that an object with working methods evaluated to false
>> just because it was a leaf in a tree. That's probably saying a lot
>> about the consistency of objects in Python generally and how a small
>> thing can be quite jarring.
>>
>> --
>> ~noufal
>> http://nibrahim.net.in
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
>
> --
> I am but a man.
>



-- 
I am but a man.


More information about the BangPypers mailing list