扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python怎么设置函数注解

扬州沐宇科技
2021-02-23 18:50:33
python

python怎么设置函数注解

在python中设置函数注解,具体方法如下:

def clip(text:str, max_len:'int > 0'=80) ->str: # 有注解的函数声明

end = None

if len(text) > max_len:

space_before = text.find(' ', 0, max_len)

if space_before >= 0:

end = space_before

else:

space_after = text.find(' ', max_len)

if space_after >= 0:

end = space_after

if end is None: # 没找到空格

end = len(text)

return text[:end].strip()

扫码添加客服微信