python函数多个参数*args和**kargs

在开发项目的过程中我们有时候会遇到函数参数个数不确定的情况

那么这个时候我们怎么来接受参数呢?

我们最容易想到的方法就是通过数组传参,这种方法是可行的,但是不够灵活也不够强大

python的设计者预想到了这种情况并且为我们提供了灵活的解决方案:args和*args

*args可以接收简单数据类型的多个参数,并组合成元组

**args可以接收键值对格式的参数并组合成为字典

比如:

`

def test(args, *kargs):

    print(kargs)

    print(args)

    

test(1,2,3,4,5,6,7,'a',True,name='tongpan',age=28)


<p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">上面的代码打印的结果分别是:
</p><p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">{‘name’: ‘tongpan’, ‘age’: 28}
</p><p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">和
</p><p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">(1,2,3,4,5,6,7)
</p><p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">当然这种便捷的写法也能用在格式化函数format上
</p><p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">传统的写法是:
</p><pre class="line-numbers language-python code-toolbar" style="padding: 1em; border: 1px dashed rgb(204, 204, 204); font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 12px; line-height: 1.5; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; vertical-align: baseline; overflow-wrap: normal; word-break: normal; box-sizing: border-box; text-shadow: white 0px 1px; tab-size: 4; background-color: rgb(245, 242, 240); hyphens: none; overflow: auto; position: relative;"><span style="color: rgb(0, 0, 0);">my_list = ['tongpan', 28]<br></br>new_str = 'some txt  name:{} ,age:{}'.format(my_list[0],my_list[1])<br></br>print(new_str)</span><br></br>

结合*args则可以直接:

my_list = ['tongpan', 28]

new_str = 'some txt  name:{} ,age:{}'.format(*my_list)

print(new_str)


<p style="margin-top: 1em; margin-bottom: 1em; padding: 0px; border: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-size: 15px; line-height: 2; font-family: normal, "Avenir Next", Avenir, "Helvetica Neue", Helvetica, "Hiragino Sans GB", 微软雅黑, sans-serif; vertical-align: baseline; overflow-wrap: break-word; word-break: break-all; box-sizing: border-box; color: rgb(30, 30, 30); white-space: normal; background-color: rgb(255, 255, 255);">非常简洁

王如棋博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论