Python中函数参数
来源:互联网 发布:mac显示网络图标 编辑:程序博客网 时间:2024/05/18 03:32
Python中函数的参数是很灵活的,下面分四种情况进行说明。
(1) fun(arg1, arg2, ...)
这是最常见的方式,也是和其它语言类似的方式
下面是一个实例:
>>> def fun(x, y): return x - y>>> fun(12, -2)14
(2) fun(arg1, arg2=value2, ...)
这种就是所谓的带默认参数的函数,调用的时候我们可以指定arg2的值,也可以省略。
下面是带默认参数的函数实例:
>>> def fun(x, y=0):return x - y>>> fun(10)10>>> fun(10, -2)12>>> fun(10, y=2)8>>> fun(x=10, y=2)8
带默认参数的函数有如上四种调用方式,但是我们调用的时候参数的名称必须和函数定义的时候的参数一致,否则会报错:
>>> fun(a=10, b=2)Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> fun(a=10, b=2)TypeError: fun() got an unexpected keyword argument 'a'
(3) fun(*arg)
即不定长参数,不管实参有多少个,在函数内部都被存放在以形参名为标识符的tuple中
下面是一个实例
>>> def fun(*param):print(param)>>> fun(12, 3, 4)(12, 3, 4)
(4) fun(**arg)
也是不定长参数,参数在函数内部将被存放在以形式名为标识符的dictionary中
下面是一个实例
>>> def fun(**param):print(param)>>> fun(sun=0, mon=1, tues=2){'sun': 0, 'mon': 1, 'tues': 2}
0 0
- Python中函数参数
- python中函数参数知识
- 详解Python中函数参数
- python中函数的参数
- Python中函数的参数
- Python中函数参数理解
- [Python] python中函数的参数定义与可变参数
- Python中函数参数的传递
- python中函数参数的传递
- python中函数参数传递*和%使用
- python 中神奇的函数参数传递
- python中可变参数函数的嵌套
- python中函数参数的传递
- Python中任意函数参数问题
- Python中可变参数的函数
- python入门笔记--函数中参数传递
- python函数中各类参数的讨论
- Python中函数参数传递问题
- java 注解
- 百度贴吧,你熟悉的网络推广基础平台
- java中多线程实现
- 线性表进阶___约瑟夫环问题
- 暴力枚举之火柴棍等式
- Python中函数参数
- unity3d学习之类和方法
- 虚拟机下ubantu的使用心得1
- 文件操作
- hdu 2059 龟兔赛跑 (dp)
- 4、Service详解(二):本地Service
- TypeError: __init__() got an unexpected keyword argument ‘maxlength’
- 多个版本jdk共存环境变量相关说明
- 搜素之任意数的拆分和