Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This expression is true in Python: {}.keys().insert(0, "") == None. Why?

Tags:

python

Look to my Python session:

>>> {}.keys().insert(0, "") == None
True

but:

>>> k = {}.keys()
>>> k
[]
>>> k.insert(0, "")
>>> k
['']

Why??

PS. Thanks for help! Python have very strange design - do not support chaining:

  • http://en.wikipedia.org/wiki/Method_chaining

That is root of my problem...

like image 696
gavenkoa Avatar asked Jun 25 '26 09:06

gavenkoa


2 Answers

list.insert returns None; when you print k you're printing the new state of the list.

like image 158
ecatmur Avatar answered Jun 27 '26 23:06

ecatmur


You are checking the return type to None in case 1 which would evaluate to True. Python insert returns None

like image 23
karthikr Avatar answered Jun 27 '26 21:06

karthikr