What are type hints in Python 3.5?

后端 未结 5 1131
死守一世寂寞
死守一世寂寞 2020-11-21 11:01

One of the most talked about features in Python 3.5 is type hints.

An example of type hints is mentioned in this article and this o

5条回答
  •  生来不讨喜
    2020-11-21 12:02

    Type-hints are for maintainability and don't get interpreted by Python. In the code below, the line def add(self, ic:int) doesn't result in an error until the next return... line:

    class C1:
        def __init__(self):
            self.idn = 1
        def add(self, ic: int):
            return self.idn + ic
        
    c1 = C1()
    c1.add(2)
    
    c1.add(c1)
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 5, in add
    TypeError: unsupported operand type(s) for +: 'int' and 'C1'
     
    

提交回复
热议问题