-3

I want to check whether a value s is exactly zero,but when s = False, s == 0 is also true.Thus how to address such case?
Thanks in advance!

Spaceship222
  • 759
  • 10
  • 20
  • 2
    `False` *is* numerically "exactly zero" in Python. You're going to need to get a clearer understanding of what you want to achieve before we can tell you how to achieve it. – user2357112 Jan 07 '20 at 03:06
  • if var 's' equals zero, s = False, .....in what context you need assistance – Bhosale Shrikant Jan 07 '20 at 03:12
  • *Why* are you trying to distinguish `False` from other zero values? Does your function take an argument and treat booleans and other numeric types differently? If so, you can check for booleans first and put numeric handling in an `elif` clause (but maybe you should redesign your function's arguments). Were you just not aware that booleans are numbers in Python? In that case, maybe you should just use `== 0` and treat False as 0. Were you just being overly defensive in your handling of a value that should always be a number? If so, maybe you don't need to handle the boolean case at all. – user2357112 Jan 07 '20 at 03:20
  • 1
    The details of why you're doing this are crucial for determining what the correct approach should be. Without more information, anything we put forward as "the right answer" or "how to do it" is likely to just hide problems and make things harder to fix down the line. – user2357112 Jan 07 '20 at 03:24

2 Answers2

1

Perhaps a bit more verbose, but you can also check that it is not False.

>>> s = False
>>> s == 0 and s is not False
False
>>> s = 0
>>> s == 0 and s is not False
True

is not works here because True and False are singletons.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • Is `False` guaranteed to be a singleton? It is a macro / effective global in Python C API, but I can't find whether that's the case in theory for the language as well. – viraptor Jan 07 '20 at 03:11
  • @viraptor yes, it is a language guaranteed singleton and this is a common idiom for checking – juanpa.arrivillaga Jan 07 '20 at 03:21
  • @juanpa.arrivillaga I was hoping for a link to reference where that's defined. The reference for built-in constants doesn't say this explicitly for example: https://docs.python.org/3/library/constants.html – viraptor Jan 07 '20 at 03:24
  • 1
    @viraptor [here's a link to the docs](https://docs.python.org/3.8/reference/datamodel.html#the-standard-type-hierarchy) "The two objects representing the values False and True are the only Boolean objects." – juanpa.arrivillaga Jan 07 '20 at 03:25
  • caveat: `s == 0` might hold for other types, e.g. objects with custom `__eq__` – Marat Jan 07 '20 at 03:30
-2

Don't use s is 0 for checking equls to 0, since 0 is not guarenteed to be singleton.

if you need it work for both int 0 and float 0.0 try the following:

type(s) in (float,int) and s ==0
hsc
  • 1,226
  • 11
  • 24
  • 1
    `s is 0` works only because of specific small-integer caching effects in cPython. For general Python code it should not be relied on. – viraptor Jan 07 '20 at 03:16
  • 1
    `0` is not a language guaranteed singlton, and in fact, it isn't always guaranteed to work this way in CPython (weird, esoteric edge cases, but they exist, see https://stackoverflow.com/questions/21456318/how-to-create-the-int-1-at-two-different-memory-locations ) – juanpa.arrivillaga Jan 07 '20 at 03:22