Python 2.x gotchas and landmines

后端 未结 23 2335
北恋
北恋 2020-11-28 17:47

The purpose of my question is to strengthen my knowledge base with Python and get a better picture of it, which includes knowing its faults and surprises. To keep things sp

23条回答
  •  孤独总比滥情好
    2020-11-28 18:17

    Python 2 has some surprising behaviour with comparisons:

    >>> print x
    0
    >>> print y
    1
    >>> x < y
    False
    

    What's going on? repr() to the rescue:

    >>> print "x: %r, y: %r" % (x, y)
    x: '0', y: 1
    

提交回复
热议问题