Is it safe to check whether a variable myvar
has not-None value by simply:
if myvar:
print('Not None detected')
I'm asking this because I have a variable and was checking whether the variable was not None
by simply if variable:
but the check has been failing. The variable contains some data but it was evaluating to False in the if
check.
Full Code:
from xml.etree import ElementTree as ElementTree
root = ElementTree.fromstring('Some xml string')
parameters = root.find('Some Tag')
udh = parameters.find('UDH')
if udh and udh.text: # In this line the check is failing, though the udh variable has value: <Element 'UDH' at 0x7ff614337208>
udh = udh.text
# Other code
else:
print('No UDH!') # Getting this output