Python中*和**的使用
在为函数传递参数和函数定义时使用参数的时候,时常会看到有和*和**,下面分别讲解其作用。
调用函数时使用*和 **
假设有函数
def test(a, b, c)
test(*args):* 的作用其实就是把序列 args 中的每个元素,当作位置参数传进去。比如上面这个代码,如果 args 等于 (1,2,3) ,那么这个代码就等价于 test(1, 2, 3) 。
test(**kwargs):** 的作用则是把字典 kwargs 变成关键字参数传递。比如上面这个代码,如果 kwargs 等于 {‘a’:1,’b’:2,’c’:3} ,那这个代码就等价于 test(a=1,b=2,c=3) 。
定义函数参数时使用*和**
def test(*args):
定义函数参数时* 的含义又要有所不同,在这里 *args 表示把传进来的位置参数都装在元组 args 里面。比如说上面这个函数,调用 test(1, 2, 3) 的话, args 的值就是 (1, 2, 3) 。:
def test(**kwargs):
类似的, ** 就是针对关键字参数和字典的了。 调用 test(a=1,b=2,c=3) 的话, kwargs 的值就是 {‘a’:1,’b’:2,’c’:3} 了。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
ajax 跨域解决
function _do_edit(id) { $.confirm({ boxWidth: '900px', title: '商品编辑', content: '<iframe src="/index.php?call=mall.spu_edit&id=' + id + '" frameborder="0" scrolling="no" id="spuform" name="spuform" style="width:95%;height:500px;"></iframe>', buttons: { close: {text: '关闭'}, exec: {text: '保存', btnClass: 'btn-blue', action: function () { window.frames["spuform"].document.forms["spuform"].submit();//跨域问题解决 location.reload(); }} }, onOpen: function () { } }); }
- 下一篇
How to Accelerate Your Python Deep Learning with Cloud GPU?
Overloaded This afternoon, I trained a 3-layers neural network as a regression model to predict the house price in Boston district with Python and Keras. The example case came from the book "Deep Learning with Python". There were 2 big loop during the running procedure. The first one went through the data for 100 times (epochs), while the second one ran 500 epochs. My poor laptop was apparently overladed in such a hot summer weather and the fan was roaring. It seems the laptop is not the best ch...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Mario游戏-低调大师作品
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Red5直播服务器,属于Java语言的直播服务器
- CentOS8编译安装MySQL8.0.19
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS6,CentOS7官方镜像安装Oracle11G