首页 > 编程语言 >Python - Function Annotations

Python - Function Annotations

时间:2024-07-30 11:17:39浏览次数:10  
标签:Function return func Python write int str type Annotations

 

def func(s:str, i:int, j:int) -> str:

return s[i:j]

The parameter s is supposed to be a string, so we place a colon after the parameter name and then write str. Parameters i and j are supposed to be integers so we write int for them.

Return type is specified in between the closing parentheses and the colon and it is preceded by an arrow. This function returns a value of type str, so we have written ->str between the closing parentheses and the colon.

We can write descriptive annotations, too. For example, if we want the user to send only values from 0 to 5 for the parameter i, then we can write the annotation in string form.

def func(s:str, i:'int 0 to 5', j:int) -> str:

return s[i:j]

Similarly, the annotation for the return type can also be descriptive. So, annotations not only document the expected types but also allow us to specify any type of metadata about the parameters and return type.

If there is default value for any parameter, then it is written after the annotation. In our function func, if we want default value for the last two parameters, we can write it like:

def func(s:str, i:'int 0 to 5'=0, j:int=3) -> str:

return s[i:j]

The function annotations are optional, and they are used just for documentation purpose, they do not enforce any type checking by the interpreter.

 

>>> func.__annotations__

标签:Function,return,func,Python,write,int,str,type,Annotations
From: https://www.cnblogs.com/zhangzhihui/p/18331973

相关文章

  • 使用带有 pythonKit XCODE 的嵌入式 Python,在 iOS 应用程序中与 OpenCV-python 签名不
    我根据Beewares使用指南在XCODE中将Python嵌入到我的iOS项目中https://github.com/beeware/Python-Apple-support/blob/main/USAGE.md运行时,我得到pythonKit找不到由ultralytics导入的cv2错误。当我将OpenCV-python添加到我的app_packages文件夹时......
  • Python - Arguments and Parameters
    ParametersinFunctionDefinitionA.deffunc(name):MatchbypositionorbynameB.deffunc(name=value):DefaultargumentC.deffunc(*args):CollectextrapositionalargumentsintuplenamedargsD.deffunc(**kwargs):Collectextrakeywordargumentsi......
  • Python MySQL 无法连接,原因不明
    当我尝试使用python连接到我的MySQL数据库时,由于未知原因显示错误:dTraceback(mostrecentcalllast):File"/usr/local/bin/flask",line8,in<module>sys.exit(main())^^^^^^File"/usr/local/lib/python3.12/site-packages/flask/cli.py&......
  • 基于Python Django的旅游景点数据分析与推荐系统
    基于PythonDjango的旅游景点数据分析与推荐系统。源码+数据库+文档(LW)。开发技术:Pythondjangomysql。项目内容:系统包括多个功能模块,涵盖了用户管理、旅游景点管理、管理员管理、系统管理等方面,以及一些其他辅助功能和信息展示模块。用户管理模块允许管理员管理系统中的用......
  • django基于Python的校园个人闲置物品换购平台
    django基于Python的校园个人闲置物品换购平台。源码+数据库+文档(lw+ppt)。开发技术:Pythondjangomysql。项目内容:系统主要包括主页、个人中心、用户管理、景点信息管理、系统管理等功能。    ......
  • Python的使用技巧整理——100个Python使用技巧代码和运行结果(上)
    整理一些更实用的Python编程技巧,这些技巧将涵盖性能优化、代码简洁性、调试和测试等方面,并提供具体的代码示例和结果。以下是详细的内容:1.列表生成表达式列表生成表达式不仅简洁,还能提高性能。#示例代码squares=[x**2forxinrange(10)]print(squares)运行结果:[......
  • Python 缓存工具统计并使用自定义密钥
    我正在寻找一种方法来使用python的cachetools内置缓存库功能,但也支持命中/未命中统计,使用自定义键函数,并且如果可能的话支持无界缓存?不幸的是,我可以只能找到这些方法:如果我想使用未绑定的缓存,并有命中/未命中统计:fromcachetools.funcimportlru_cache......
  • 如何用Python从PDF文件中抓取数据
    我想抓取此PDF第7页中的数据,然后移至数据框,然后移至CSV。您能提供同样的帮助吗?当然,我可以帮。以下是用Python从PDF文件中抓取数据并将数据保存到CSV文件的步骤:1.安装必要的库需要安装以下Python库:PyPDF2:用于读取P......
  • python读取大型二进制文件最有效的方法是什么
    我有一个大(21GB)文件,我想将其读入内存,然后传递给一个子例程,该子例程对我透明地处理数据。我在Centos6.5上使用python2.6.6,因此无法升级操作系统或python。目前,我正在使用f=open(image_filename,"rb")image_file_contents=f.read()f.close()transparent_subrout......
  • Python:为列表中的每个类对象创建一个不同的副本
    如何制作Python类中对象列表的副本,以便每个副本都是所述Python类的不同实例?假设我有一个Python类classmyClass():def__init__(self,neighbor):self.neighbor=neighbor另外假设myList=[a,b,c,d,...]是一个列表myClass对......