• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python中函数传参详解

python中函数传参详解

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了python开发技术详解,python os模块详解,python正则表达式详解,python 字典详解,python类详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、参数传入规则

可变参数允许传入0个或任意个参数,在函数调用时自动组装成一个tuple;

关键字参数允许传入0个或任意个参数,在函数调用时自动组装成一个dict;

1. 传入可变参数:

 def calc(*numbers):
   sum = 0
   for n in numbers:
     sum = sum + n * n
   return sum
</div>

以上定义函数,使用如下:

传入多个参数,

calc(1, 2, 3, 4)
30 #函数返回值
</div>

传入一个列表,

nums = [1, 2, 3]
calc(*nums) # 通过 * 将list中的元素作为可变参数传入函数
14 # 函数返回值

</div>

2.传入关键字参数:

>>> def person(name, age, **kw):
...   print('name: ', name, 'age: ', age, 'other: ', kw)
... 
>>> 
>>> person('luhc', 24, city='Guangzhou')
name: luhc age: 24 other: {'city': 'Guangzhou'}

</div>

同样,可以将预先定义的dict作为参数传入以上函数:

>>> info = {'city': 'Guangzhou', 'job': 'engineer'}
>>> 
>>> person('luhc', 24, **info)
name: luhc age: 24 other: {'city': 'Guangzhou', 'job': 'engineer'}
</div>

注意: 函数person 获得的是参数 info 的一份拷贝,在函数内修改不会影响 info 的值

3. 在关键字参数中,可以限制关键字参数的名字:

# 通过 * 分割,以指定关键字参数名
>>> def person(name, age, *, city, job):
...   print('name: ', name, 'age: ', age, 'city: ', city, 'job: ', job)
... 
>>> 
>>> person('luhc', 24, city='Guangzhou', job='engineer')
name: luhc age: 24 city: Guangzhou job: engineer

# 如果传入参数中,存在参数名不在定义的范围内,将抛出异常
>>> person('luhc', 24, city='Guangzhou', jobs='engineer')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: person() got an unexpected keyword argument 'jobs'
>>>

</div>

此外,如果函数中已经指定可变参数,则 * 可以省略,如下:

# 省略了用 * 作为分割,指定关键字参数名
>>> def person(name, age, *args, city, job):
...   print('name: ', name, 'age: ', age, 'args: ', args, 'city: ', city, 'job: ', job)  
... 
>>> 
>>> person('luhc', 24, 'a', 'b', city='Guangz', job='engineer')
name: luhc age: 24 args: ('a', 'b') city: Guangz job: engineer
>>> 
# 同样,如果传入了关键字参数未指定的参数名,则抛出异常
>>> person('luhc', 24, 'a', 'b', city='Guangz', job='engineer', test='a')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: person() got an unexpected keyword argument 'test'
>>>

</div>

二、参数组合使用:

参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数

def f1(a, b, c=0, *args, **kw):
  print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
  print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

</div>

 以上就是本文给大家介绍的全部内容了,希望能够对大家理解Python的函数参数的传递有所帮助

</div>

您可能想查找下面的文章:

  • python中OrderedDict的使用方法详解
  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • Python 操作MySQL详解及实例
  • python中OrderedDict的使用方法详解
  • Python中异常重试的解决方案详解
  • 详解python 字符串和日期之间转换 StringAndDate
  • Python 操作MySQL详解及实例
  • Python 登录网站详解及实例
  • 详解Python中类的定义与使用

相关文章

  • Python实现的彩票机选器实例
  • 详细介绍Python函数中的默认参数
  • python实现应用程序在右键菜单中添加打开方式功能
  • 解决pyqt中ui编译成窗体.py中文乱码的问题
  • Python中用函数作为返回值和实现闭包的教程
  • Python实现文件复制删除
  • Python Mysql数据库操作 Perl操作Mysql数据库
  • python制作websocket服务器实例分享
  • Python基于有道实现英汉字典功能
  • tensorflow模型保存、加载之变量重命名实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 在Python的Flask中使用WTForms表单框架的基础教程
    • 举例详解Python中threading模块的几个常用方法
    • Python多线程经典问题之乘客做公交车算法实例
    • python中global用法实例分析
    • Python 搭建Web站点之Web服务器网关接口
    • Python内建数据结构详解
    • Python多进程并发(multiprocessing)用法实例详解
    • Python中文分词工具之结巴分词用法实例总结【经典案例】
    • TensorFlow dataset.shuffle、batch、repeat的使用详解
    • Python减少循环层次和缩进的技巧分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有