[Python] Why must I cast int to bool before checking "is True"?

Discussão em 'Python' iniciado por Stack, Agosto 22, 2025.

  1. Stack

    Stack Membro Participativo

    In my url.py file in Django, I have the following line:

    if bool(settings.DEBUG) is True:


    This works well because settings.DEBUG == 1 therefore it equals True after the bool() function. However, I needed to add the bool() to make it work. The original line was not working and the original line was:

    if settings.DEBUG is True:


    This always evaluates to False, even when settings.DEBUG == 1.

    Why is the value 1 evaluating to False when I do not cast it explicitly to bool?

    In other words, why doesn't "1 is True" evaluate true?

    Note that I am using python 3 in case that matters to the answer.

    Continue reading...

Compartilhe esta Página