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

后端 未结 4 1956
死守一世寂寞
死守一世寂寞 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-22 23:49

    What you have is a float literal without the trailing zero, which you then access the __truediv__ method of. It's not an operator in itself; the first dot is part of the float value, and the second is the dot operator to access the objects properties and methods.

    You can reach the same point by doing the following.

    >>> f = 1.
    >>> f
    1.0
    >>> f.__floordiv__
    
    

    Another example

    >>> 1..__add__(2.)
    3.0
    

    Here we add 1.0 to 2.0, which obviously yields 3.0.

提交回复
热议问题