Python 2.x gotchas and landmines

后端 未结 23 2276
北恋
北恋 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:30

    Floats are not printed at full precision by default (without repr):

    x = 1.0 / 3
    y = 0.333333333333
    print x  #: 0.333333333333
    print y  #: 0.333333333333
    print x == y  #: False
    

    repr prints too many digits:

    print repr(x)  #: 0.33333333333333331
    print repr(y)  #: 0.33333333333300003
    print x == 0.3333333333333333  #: True
    

提交回复
热议问题