-1

In my code there's a variable foo that receives the value from a called function. Because of the parameters passed to this function it returns None.

When i execute an if comparing the variable foo with the None value it returns False. But if i sign type(None) to a bar variable and execute the same ifit returns True.

Why?

TL;DR

foo = function()
type(foo)
>> <class 'NoneType'>

if foo == None
>> False

bar = None
if foo == bar 
>> False

bar = type(None)
if foo == bar
>> True

Why?

  • 1
    with `def function(): return None` I get True for `foo = function(); print(foo == None)` and also True for bar = None; `print(foo == bar)` – DarrylG Jan 14 '20 at 18:14
  • The posted code isn't valid python, so presumably there's additional relevant code that got edited out. From the looks of what's left, somewhere there's a `foo=type(foo)` or something similar. – manveti Jan 14 '20 at 18:17

1 Answers1

0

The None object denotes the lack of a value. For the purposes of these operators, the lack of a value indicates that the remainder of the expression also lacks a value and should not be evaluated.

https://www.python.org/dev/peps/pep-0505/

scottsaenz
  • 90
  • 6