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?
-
1The output is correct, what else were you expecting?Cory Kramer– Cory Kramer2014年11月14日 14:05:50 +00:00Commented Nov 14, 2014 at 14:05
-
2possible duplicate of Python: Checking if a 'Dictionary' is empty doesn't seem to workAnshul Goyal– Anshul Goyal2014年11月14日 14:06:46 +00:00Commented Nov 14, 2014 at 14:06
-
1we don't know what to explain. please clarify!Karoly Horvath– Karoly Horvath2014年11月14日 14:07:46 +00:00Commented Nov 14, 2014 at 14:07
-
2Downvoted on account that you don't include what you think the expected behaviour is.wvdz– wvdz2014年11月14日 14:08:06 +00:00Commented Nov 14, 2014 at 14:08
-
2because they are different conditions and statements. do you expect the same thing will happen no matter what you type?Karoly Horvath– Karoly Horvath2014年11月14日 14:13:08 +00:00Commented Nov 14, 2014 at 14:13
4 Answers 4
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
3 Comments
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.I see nothing weird in your output. Let's go step-by-step:
ais dictionary, more specifically a dictionary object;ais a dictionary, but it's empty, so its truth value isFalse
Therefore:
- The first
if, sinceaisFalse, prints theelsestatement and that's right; - The second
if, sincenot aevaluates toTruebecauseaisFalse, prints theifpart and that's right too. - Last, but not least
ais not aNoneobject, but adictobject, so it's right too that theelsepart is taken and printed.
2 Comments
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.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
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