Fork me on GitHub

Python系列文章-python函数参数总结

目录

  • 背景

  • 第一部分 第一个项目

  • 第二部分 总结

  • 参考文献及资料

背景

Python和其他编程语言一样函数也有参数的概念,所以也有形参(Parameters)和实参(Arguments)的概念。

Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what kind of arguments a function can accept.

例如下面的函数:

1
2
def func(foo, bar=None, **kwargs):
pass

foo, bar and kwargs are parameters of func. However, when calling func, for example:

1
func(42, bar=314, extra=somevar)

the values 42, 314, and somevar are arguments.

https://levelup.gitconnected.com/5-types-of-arguments-in-python-function-definition-e0e2a2cafd29

five types of parameters and two kinds of arguments

参数定义角度:

  • 必选参数:调用函数必须指定参数值;
  • 可选参数:在函数定义时给定一个初始值,在函数调用时可以不传这个参数,采用默认参数的值;

函数调用角度:

  • 位置参数
  • 可变参数
  • 关键字参数:

第一部分

参考文献及资料

1、变量官网介绍:

0%