Python: str(None) should return ” not ‘None’

Think about the following code:

x = some_function_that_might_return_none()
if str(x):
  do_one_thing()
else:
  do_another_thing()

do you think do_another_thing() will ever execute?

Did you know that str(None) is ‘None’ and thus:

if str(None):
  this_willl_always_execute()

I can see how you might want str(None) to be ‘None’ for debugging display, etc., but to preserve the semantics of “if” I think it should be ” or some other value that fails a simple if statement.

This entry was posted in General and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>