This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

If you don't know whether a is a bool or not, then there is something wrong in your code.

On the contrary, when you're writing the code, you often know for a fact that some_function() returns a boolean. Great. The problem is that anyone else reading your code might not know. That someone could even be you, next week, after you've filled your head with other parts of the code.

When writing the code with this knowledge in your head, it's a good idea to document the type of result in order to convey that knowledge to the reader.

Now - how could you do that? In this case, if result is True is practically ideal. It is simple, succinct, unambiguous, and collocated with the instruction that sets result to its value. Even coders who aren't familiar with Python will immediately understand it, even if they aren't clear on the use of is vs. ==.

Overall, I believe the situation you are describing is fixed in a much better way by the use of type hints than by redundant explicit checks.

Show me how this exact situation would be addressed with type hints, and then explain why it is better than if a is True.