函数的参数类型提示

#(最低 Python 版本为 3.5)

可以在定义函数的时候以下面的格式

def register(形参:类型)->返回值:

使传参类型错误的时候得到提示

注意

类型错误并不会报错,只是提示作用

最低 Python 版本为 3.5

#                "haha"    22  ('play','music')->返回值类型111
def register(name:str,age:int,hobbbies:tuple)->int:
 print(name)
 print(age)
 print(hobbbies)
 return 111

register(1,'aaa',[1,])
res=register('egon',18,('play','music'))



def register(name:str='egon',age:int=18,hobbbies:tuple=(1,2))->int:
 print(name)
 print(age)
 print(hobbbies)
 return 111

# register(1,'aaa',[1,])
# res=register('egon',18,('play','music'))
res=register()


def register(name:"必须传入名字",age:1111111,hobbbies:"必须传入爱好元组")->"返回的是整型":
 print(name)
 print(age)
 print(hobbbies)
 return 111

# register(1,'aaa',[1,])
# res=register('egon',18,('play','music'))
# res=register('egon',19,(1,2,3))

print(register.__annotations__)
 posted on 2020-03-29 15:39  wwwpy  阅读(185)  评论(0编辑  收藏  举报