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?

13
  • 1
    The output is correct, what else were you expecting? Commented Nov 14, 2014 at 14:05
  • 2
    possible duplicate of Python: Checking if a 'Dictionary' is empty doesn't seem to work Commented Nov 14, 2014 at 14:06
  • 1
    we don't know what to explain. please clarify! Commented Nov 14, 2014 at 14:07
  • 2
    Downvoted on account that you don't include what you think the expected behaviour is. Commented Nov 14, 2014 at 14:08
  • 2
    because they are different conditions and statements. do you expect the same thing will happen no matter what you type? Commented Nov 14, 2014 at 14:13

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

+1. I wanted to edit it in my answer, but you beat me to it, good job!
saying that they are evaluated as false is misleading. only in boolean context.
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.
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.

2 Comments

<<<<a is a dictionary, but it's empty, so its truth value is False>>>> when I say a={}, does it means that it's not allocated any memory until I add an item to it?
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.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.