使用类型标注

有时PyCharm无法判断变量类型,此时PyCharm自动补全功能失效。
使用类型标注来告诉PyCharm变量类型,从而使PyCharm提供自动补全。

型标注的格式:

变量名: 类型 = 值

age: int = 24 # 定义一个变量age,它的类型为int,值为24


def test(name: str, age: int=25, other_info: dict=None): # test函数接收两个参数,第一个参数name是str类型,第二个参数age是int型并且默认值为25,并且第三个参数other_info是字典,默认值为None
age = detail_info['age']

  

 

posted @ 2023-06-05 16:46  sangern  阅读(12)  评论(0)    收藏  举报