2

I would like to know if there is any difference in 2 of below and if yes what is it and which should we use.

foo = 9

if not foo is None:
    print('not foo is None')

vs

foo = 9

if foo is not None:
    print('foo is not None')
MrZakbug
  • 53
  • 5

2 Answers2

2

There is absolutely no difference in the two. They will run and compile in the very same way. You can check the same in their performance as well there is absolutely no difference whatsoever.

Inder
  • 3,711
  • 9
  • 27
  • 42
0

Lets evaluate the conditional:

1)  if not foo(if foo is false) is None (is false):

false is false... so therefore it will print the string.

2) if foo (being true) is not None(true)

true is true ..... so therefor it will print the string.

the conditionals are both true and will both print

sjdm
  • 591
  • 4
  • 10
  • 1
    Sorry, but I honestly can't figure out what your evaluation is intended to show. You've written that in a **very** confusing way. That said, I'm 70% sure that it's wrong. – Aran-Fey Jul 03 '18 at 07:38