2

Sometimes python seems to treat them as the same whereas other times it returns False when False is used but returns nothing with None. It seems to behave very differently to null in other languages.

Some specific examples are:

  • True and None returns nothing
  • False and None returns False
Gordon
  • 19,811
  • 4
  • 36
  • 74
Mobyh
  • 111
  • 1
  • 10
  • 3
    Could you give some examples of this behaviour? It might help explain things – sacuL Nov 07 '18 at 22:34
  • 4
    In a boolean sense, both are False. – mkrieger1 Nov 07 '18 at 22:37
  • 2
    `None` is a singleton. `False` is a boolean, which in Python is an int. Both are falsey. What are you unsure about? – khelwood Nov 07 '18 at 22:37
  • 2
    See also: [What is Truthy and Falsy in python? How is it different from True and False?](https://stackoverflow.com/q/39983695/6779307) – Patrick Haugh Nov 07 '18 at 22:41
  • 1
    @khelwood `False` is also a singleton. – wim Nov 07 '18 at 22:42
  • However similar they may seem, they’re used differently. `True`/`False` are a complimentary pair for indicating yes/no answers. `None` is a third value indicating “none of the above”, “no value”. – deceze Nov 07 '18 at 22:45
  • Thanks @PatrickHaugh I'll check it out – Mobyh Nov 07 '18 at 23:02
  • @wim No it isn't. A singleton is the sole instance of its type. False is a instance of a type with two elements: True and False. – khelwood Nov 08 '18 at 07:20
  • @khelwood Seems you're lugging baggage over from Java. From [PEP 285](https://www.python.org/dev/peps/pep-0285/), which added the bool type to the language: *The values `False` and `True` will be singletons, like `None`.* The datamodel does not allow another instance of `False` is the important property here (e.g. bool can't be subclassed), not whether it's the sole instance of the type. – wim Nov 08 '18 at 15:48
  • @wim I stand corrected. I didn't realise that Python had its own special definition of singleton. – khelwood Nov 08 '18 at 22:32
  • To address the specific examples added after the very general and authoritative answer was given below, `and` does not cast to boolean: `and` will return the first operand that was falsy, or the right hand side if both operands were truthy. – Gordon May 08 '22 at 21:12

1 Answers1

19

Different values in Python can be described as being "truthy" or "falsy" even if they aren't Boolean values, which means they are interpreted as True or False in a situation that expects a Boolean value (such as an if condition). As defined in the documentation, every value in Python, regardless of type, is interpreted as being True except for the following values (which are interpreted as False):

  • Constants defined to be false: None and False.
  • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty sequences and collections: '', (), [], {}, set(), range(0)

To your specific situation, using the if situation, the following statement:

if None:
    # some code here

would be functionally identical to:

if False:
    # some code here

This is because, as shown in the list above, the value None is automatically converted to False for the purposes of the if condition. This is something referred to as "syntactic sugar", which is a feature of the language that exists to make the developer's life easier.

However, just because None is interpreted as False in this particular scenario, that doesn't mean the two values are equal to each other. This is because False is meant to be part of the True/False pair indicating binary concepts like "yes/no", "on/off", etc. None, on the other hand, represents the concept of nothing. Variables with a value of None means they have no value at all. To compare it to False in the form of a metaphor, False would be like answering somebody by saying "No", where None would be like not answering them at all.

As a more practical example, see the following code snippet:

if None == False:
    # code in here would not execute because None is not equal to False
Abion47
  • 22,211
  • 4
  • 65
  • 88
  • I would not call automatic type conversion "syntactic sugar", as it eliminates a syntax (e.g. bool(None)) rather than replaces it with a sweeter one... and automatic type conversions are common in most programming languages, without being labeled as a sweetener. – kmarsh Oct 29 '20 at 14:14
  • @kmarsh Syntactic sugar doesn't necessarily mean that a simpler syntax gets converted to a more verbose syntax by the compiler. In the purest sense, syntactic sugar is merely a syntax feature that is intended to make the developer's job easier from either a functional or cosmetic standpoint compared to an alternative existing convention. Truthiness of non-boolean values definitely falls into that category, in Python and in other languages. And besides, it does replace `if bool(x):` with a sweeter version `if x:`. – Abion47 Oct 29 '20 at 19:23
  • Then all automatic type conversions are "syntactic sugar", which I reject. – kmarsh Oct 29 '20 at 21:03
  • @kmarsh Reject it if you want, but I would argue that all automatic type conversions **are** syntactic sugar because they are a syntactic feature that saves the developer from having to explicitly convert a value of a given type to another unrelated type, such as booleans and lists (which would otherwise be necessary even in dynamically-typed languages). Just because a feature has become a staple in a lot of modern interpreted languages doesn't mean it isn't syntactic sugar. – Abion47 Oct 29 '20 at 21:19