What is `1..__truediv__` ? Does Python have a .. (“dot dot”) notation syntax?

后端 未结 4 1945
死守一世寂寞
死守一世寂寞 2020-12-22 23:33

I recently came across a syntax I never seen before when I learned python nor in most tutorials, the .. notation, it looks something like this:

         


        
4条回答
  •  天命终不由人
    2020-12-23 00:05

    Two dots together may be a little awkward at first:

    f = 1..__truediv__ # or 1..__div__ for python 2
    

    But it is the same as writing:

    f = 1.0.__truediv__ # or 1.0.__div__ for python 2
    

    Because float literals can be written in three forms:

    normal_float = 1.0
    short_float = 1.  # == 1.0
    prefixed_float = .1  # == 0.1
    

提交回复
热议问题