Python - Any way of checking if number 0 or False

前端 未结 1 1123
故里飘歌
故里飘歌 2021-01-22 10:10

If I were to set a variable as False, it is read as being equal to zero. Is there any way I can carry out a check to see if the variable if actually False or if it\'s the number

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 10:21

    You can test if the object is the False singleton value, by testing for identity:

    if Spam is False:
    

    You should really refactor your code to not have to rely on the type here; None is a better false-y sentinel value to use, yes.

    In general, use None as a sentinel meaning 'no value set', or if you should support None as well, use a unique object as a sentinel:

    _sentinel = object()
    
    # ...
    
    
    if option is _sentinel:
        # no value set
    

    0 讨论(0)
提交回复
热议问题