4

I'm having trouble understanding this

I tried:

if not None:
    print('True')

Why does it print True? Isn't the None type supposed to be None?

SteveLacy
  • 4,150
  • 2
  • 23
  • 30
Aaron
  • 445
  • 1
  • 4
  • 11
  • Read this: http://stackoverflow.com/questions/3289601/null-object-in-python – Mestica Aug 02 '16 at 19:43
  • @Ralf17 That thread did not answer my question. – Aaron Aug 02 '16 at 19:52
  • I imagine that you are getting down voted for a couple of reasons. 1) It doesn't appear that you have done a simple google search on your question to find the python documentation which very clearly states that `None` is evaluated as `False`. 2) Proofread. "Why does it True?" is pretty sloppy grammar. – Alex Jadczak Aug 02 '16 at 19:56
  • @Aaron I recommend that you should start reading a Python book to understand basic programming concepts. What you are asking is as simple and analogous to Arithmetic in Math. You can also go to https://www.python.org/doc/ to learn more about Python. – Mestica Aug 02 '16 at 19:57
  • @Ralf17 Im learning from diveintopython3 and LPTHW. – Aaron Aug 02 '16 at 21:42
  • @Check I am a new user, of course I will make mistakes, the least you can do is cut me some slack and tell me what I did before downvoting. And for the grammar, I moved the 'print' out of "Why does it print True" when moving around some text. – Aaron Aug 02 '16 at 21:45
  • Possible duplicate of [Why is "if not someobj:" better than "if someobj == None:" in Python?](http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python) – Łukasz Rogalski Aug 03 '16 at 08:49
  • @Aaron yeah, proofread your text a bit. Use a quote in "I'm". Put in some effort – noɥʇʎԀʎzɐɹƆ Aug 03 '16 at 16:50
  • Well. it prints `True` because you literally tell the program to print "True". –  Oct 18 '17 at 12:43
  • Being used to SQL's NULL I find this question rather relevant. – P-Gn May 29 '18 at 15:08

5 Answers5

7

All Python objects have a truth value, see Truth Value Testing. That includes None, which is considered to be false in a boolean context.

In addition, the not operator must always produce a boolean result, either True or False. If not None produced False instead, that'd be surprising when bool(None) produces False already.

The None value is a sentinel object, a signal value. You still need to be able to test for that object, and it is very helpful that it has a boolean value. Take for example:

if function_that_returns_value_or_None():

If None didn't have a boolean value, that test would break.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
4

Python Documentation

4.1. Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

None

False

zero of any numeric type, for example, 0, 0.0, 0j.

any empty sequence, for example, '', (), [].

any empty mapping, for example, {}.

instances of user-defined classes, if the class defines a bool() or len() method, when that method returns the integer zero or bool value False.

Community
  • 1
  • 1
Alex Jadczak
  • 550
  • 3
  • 11
3

In Python None is a singleton. It is called the null in other languages.

In your if not None:, the compiler assumes that not None means non empty, or non-zero and we know an if statement evaluates non-zero values as True and executes them.

Function Examples:

1) if not None: prints argument x in test()

   def test(x):
       if not None:
           print(x)

   >>> test(2)
   2

2) if 1: prints argument x in test()

   def test(x):
       if 1:
           print(x)

   >>> test(2)
   2

3) if -1: prints argument x in test()

   def test(x):
       if -1:
           print(x)

   >>> test(2)
   2

4) if 0: does not prints argument x in test()

   def test(x):
       if 0:
           print(x)

   >>> test(2)

5) if True: prints argument x in test()

   def test(x):
       if True:
           print(x)

   >>> test(2)
   2
Mestica
  • 1,489
  • 4
  • 23
  • 33
  • 1
    `None` is not the same thing as `null` in other languages. `null` is the absence of any value, `None` is itself a value. Python references always exist, and need something to be pointing to. The `None` object is a useful default. The Python compiler makes no assumptions about `None` whatsoever, it is the object itself that implements the `nb_bool` slot (the C-API equivalent of the [`__bool__` method](https://docs.python.org/3/reference/datamodel.html#object.__bool__), returning `0` (false). The pile of objects that are falsey is much larger than the examples you have given. – Martijn Pieters Mar 12 '18 at 07:31
1

Each value has a property known as "truthiness". The "truthiness" of None is False. This is so for several reasons, such as clean code when you consider a return value of None to be failure or False.

"Empty" objects like '', [], 0, or {} all evaluate to false. Note that this doesn't include objects like 'None' (the string) or '0'.

So if not None converts None to False.

"Truthiness" is also known as "booleaness", which is more formal in some contexts.

noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
0

[Irony mode on]

If you are not happy printing True you can make it print False:

if not None:
    print('False')

Now it prints False :)

EDIT: If you are worried about why it doesn't print None instead of True or False (or Apples) you can just make it print None:

if not None:
    print('None')