Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid conversion xml.etree.ElementTree.Element object to boolean #47445

Closed
xaka mannequin opened this issue Jun 25, 2008 · 3 comments
Closed

invalid conversion xml.etree.ElementTree.Element object to boolean #47445

xaka mannequin opened this issue Jun 25, 2008 · 3 comments

Comments

@xaka
Copy link
Mannequin

xaka mannequin commented Jun 25, 2008

BPO 3195
Nosy @pitrou

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-06-25.12:27:49.772>
created_at = <Date 2008-06-25.09:09:15.257>
labels = ['expert-XML', 'invalid']
title = 'invalid conversion xml.etree.ElementTree.Element object to boolean'
updated_at = <Date 2008-06-25.12:27:49.761>
user = 'https://bugs.python.org/xaka'

bugs.python.org fields:

activity = <Date 2008-06-25.12:27:49.761>
actor = 'benjamin.peterson'
assignee = 'none'
closed = True
closed_date = <Date 2008-06-25.12:27:49.772>
closer = 'benjamin.peterson'
components = ['XML']
creation = <Date 2008-06-25.09:09:15.257>
creator = 'xaka'
dependencies = []
files = []
hgrepos = []
issue_num = 3195
keywords = []
message_count = 3.0
messages = ['68720', '68721', '68723']
nosy_count = 3.0
nosy_names = ['pitrou', 'mishok13', 'xaka']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue3195'
versions = ['Python 2.5']

@xaka
Copy link
Mannequin Author

xaka mannequin commented Jun 25, 2008

Python 2.5.2 (r252:60911, May  7 2008, 15:19:09)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree.ElementTree import Element
>>> e = Element('Test', {'attr' : 'value'})
>>> b = not e
>>> print b
True

Why i'm getting True here instead of False?
Because of this i can not do:
if not e:
# here some logic
pass

@xaka xaka mannequin added the topic-XML label Jun 25, 2008
@mishok13
Copy link
Mannequin

mishok13 mannequin commented Jun 25, 2008

To quote Python Library Reference, paragraph 3.1:
"""Any object can be tested for truth value, for use in an if or while
condition or as operand of the Boolean operations below. The following
values are considered false:
[skipped]

  • instances of user-defined classes, if the class defines a
    __nonzero__() or __len__() method, when that method returns the integer
    zero or bool value False.
    """
    And back to python console:
    In [112]: from xml.etree.ElementTree import Element

In [113]: e = Element('foo', {'key': 'value'})

In [114]: len(e)
Out[114]: 0

In [115]: not e
Out[115]: True

This is because Element is just a container and acts more like a list.
So, if you actually append items to this list-like structure, you'll get
this:

In [116]: e.append(Element('bar', {42: 1337}))

In [117]: e.append(Element('baz', {'whatever': 'wherever'}))

In [118]: len(e)
Out[118]: 2

In [119]: not e
Out[119]: False

In conclusion, this just doesn't look like a bug to me. You could try
using "if e is not None" form.

@pitrou
Copy link
Member

pitrou commented Jun 25, 2008

Indeed, this is not a bug, although it can be misleading.
Generally, if you test for None, it is better to write "if x is None",
it is at the same time more accurate, more explicit for someone reading
your code, and also executes faster than "if not x".

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants