0

I'm new to Python and I am a bit confused with the way Python treats an empty object.

Consider this code piece;

a = {}

if a:
    print "a is alive!"
else:
    print "a is NOT alive!"

if not a:
    print "NOT a!"
else:
    print "a!"

if a is None:
    print "a is None!"
else:
    print "a is NOT None!"

I get the following output for this code piece.

a is NOT alive!
NOT a!
a is NOT None!

Edit::

I am under the assumption that an object initialized by {} is a Valid Object. Why doesn't Python treat it that way? and why do I get diff output for diff If conditions?

Edit 2::

In C++, when I say

 Object obj;
 if (obj){
 }

It will enter the IF block if obj is NOT NULL(regardless if it is garbage value or whatever)

But the same thing when I translate to python.

a = {} #This is a valid object
if a:
# Doesn't work!

Why? and I read Python evaluates {} as False. Why is that?

Syed Mauze Rehan
  • 1,125
  • 14
  • 31

4 Answers4

2

Empy dict/sets/lists etc are evaluated as false. None is its own type, with only one possible value.

https://docs.python.org/2.4/lib/truth.html Tells you what is evaluated as true and false

horns
  • 1,843
  • 1
  • 19
  • 26
  • +1. I wanted to edit it in my answer, but you beat me to it, good job! – Rusty Nov 14 '14 at 14:13
  • 2
    saying that they are evaluated as false is misleading. only in *boolean* context. – Karoly Horvath Nov 14 '14 at 14:15
  • 1
    The key difference is that you are using `is` None. So not only is an empty dictionary not *equal* to None, it is also not exactly the same object. For the same reason `{} == {}` evaluates as True, but `{} is {}` evaluates False - they are both empty dicts, but they are not the *same* empty dictionary. – Tom Dalton Nov 14 '14 at 14:23
1

It is a valid python object, but it is empty, so it is treated as a False, the same goes for lists [] or numbers 0. You do have a dict, but it is not a None object.

Rusty
  • 904
  • 4
  • 10
1

I see nothing weird in your output. Let's go step-by-step:

  • a is dictionary, more specifically a dictionary object;
  • a is a dictionary, but it's empty, so its truth value is False

Therefore:

  • The first if, since a is False, prints the else statement and that's right;
  • The second if, since not a evaluates to True because a is False, prints the if part and that's right too.
  • Last, but not least a is not a None object, but a dict object, so it's right too that the else part is taken and printed.
Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
  • <<<>>> when I say a={}, does it means that it's not allocated any memory until I add an item to it? – Syed Mauze Rehan Nov 14 '14 at 14:24
  • If you do `a={}` memory is allocated (on my system 280 bytes are allocated); you can check doing `sys.getsizeof(object)`. The truth value of the dictionary is determined from the fact that is empty. It's a pythonic behaviour and it works also on empty strings, lists, sets and so on. – Alberto Coletta Nov 14 '14 at 14:35
0

with

a = {}

you are creating an dictionary object which is not NoneType you can check the class of your object with

type(a)

which will give you:

type dict

if not a:

will return False if a has already any members and True if a is just and empty list

arash javanmard
  • 1,362
  • 2
  • 17
  • 37