首页 > 编程语言 >What does -> mean in Python function definitions?

What does -> mean in Python function definitions?

时间:2024-03-02 21:55:54浏览次数:19  
标签:function What Python does definitions mean

What does -> mean in Python function definitions?

In Python, the "->" symbol is used to indicate the return type of a function. It is part of the function definition in a Python 3.5 or later. For example, the following code defines a function add that takes in two arguments, a and b, and returns the sum of the two arguments.


def add(a: int, b: int) -> int:
    return a + b

The -> int at the end of the function definition indicates that the function returns an integer. This is known as type hinting and is used to help with code readability and debugging.
Please note that this feature is not enforced in Python and it's just used as a hint for developer and IDEs.

It's a function annotation.

In more detail, Python 2.x has docstrings, which allow you to attach a metadata string to various types of object. This is amazingly handy, so Python 3 extends the feature by allowing you to attach metadata to functions describing their parameters and return values.

There's no preconceived use case, but the PEP suggests several. One very handy one is to allow you to annotate parameters with their expected types; it would then be easy to write a decorator that verifies the annotations or coerces the arguments to the right type. Another is to allow parameter-specific documentation instead of encoding it into the docstring.

Reference

  1. What does -> mean in Python function definitions?

  2. What does -> mean in Python function definitions?

标签:function,What,Python,does,definitions,mean
From: https://www.cnblogs.com/guanghui-hua/p/18049300

相关文章

  • python推导式(comprehension)
    #推导式#1.zip()函数##功能:将多个迭代器Iterator对象或者可迭代对象中的元素压缩在一起,返回一个zip对象##zip对象既是一个可迭代对象也是一个迭代器对象names=["Tony","Tom","Gray","Lisa"]grades=[11,22,31,21]chart1=zip(names,grades)print(list(ch......
  • python 正则表达式
    正则表达式(regularexpression)关于这个知识点菜鸟教程上介绍的很详细还有一个是介绍各种语言的正则表达式的正则表达式是一个特殊的字符序列,利用事先定义好的一些特定字符以及它们的组合组成一个“规则”,检查一个字符串是否与这种规则匹配来实现对字符的进行过滤。正则表达......
  • Selenium自动化爬取网页数据——Python实现
    Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE,MozillaFirefox,Safari,GoogleChrome,Opera,Edge等。这个工具的主要功能包括:测试与浏览器的兼容性——测试应用程序看是否能够很好得工作在不同浏览器和操作......
  • python的基础知识
    python的保留字与运算符的区别保留字是Python语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量、函数、类、模板以及其他对象命名。在实际开发中,如果使用Python中的保留字作为标识符,则解释器会提示“invalidsyntax”的错误......
  • python面向对象(Object Oriented Program)
    面向对象(ObjectOrientedProgram)在Python中,所有的数据(包括数字和字符串)实际都是对象,同一类型的对象都有相同的类型。我们可以使用type()函数来获取关于对象的类型信息。什么是类,什么是对象?在Python中,使用类来定义同一种类型的对象。类(class)是广义的数据类型,能够定义复......
  • 异常(异常是python对象) 和 自定义异常类
    Python提供了异常和断言来处理程序在运行过程中出现的异常和错误什么是异常?分清楚程序发生异常和程序执行错误,它们完全是两码事,程序由于错误导致的运行异常,是需要程序员想办法解决的;但还有一些异常,是程序正常运行的结果,比如用raise手动引发的异常。异常是在程序执行过程中......
  • python一些技巧
    1.一个for循环的一个非常好用的例子示例如下:for_inrange(10):print("Helloworld!")2.Python中变量名后面加冒号,函数后面加箭头deff(text:str,max_len:'int>0'=80)->str:returnTrue函数声明中,text:strtext是参数:冒号后面str是参数的注释。如果......
  • python中的各种下划线
    ----------------------------核心风格:避免用下划线作为变量名的开始。---------------单下划线开始的成员变量叫做保护变量,意思是只有类对象和子类对象自己能访问到这些变量;但这只是一个约定,就像是道德约束,并没有法律效力双下划线开始的是私有成员,意思是只有类对象自己能访......
  • python取反操作符的解释
    今天看做词云的代码看到这样一句words_df=words_df[~words_df.segment.isin(stopwords.stopword)]刚开始看不懂这个"~",就去百度了一下,记录下来(1)在计算机里面,负数是以补码存储的(2)原码求补码:取反,+1(3)补码求原码:取反,+1(4)取反操作是在原码上进行的!实际的计算......
  • Python 中的 if __name__ == '__main__' 该如何理解
    结论if__name__=='__main__'我们简单的理解就是:如果模块是被直接运行的,则代码块被运行,如果模块是被导入的,则代码块不被运行。程序入口对于很多编程语言来说,程序都必须要有一个入口,比如C,C++,以及完全面向对象的编程语言Java,C#等。如果你接触过这些语言,对于程序入口这个概......