嗨, 大家好: 网站在新年后正式开通,欢迎大家加入我们,共同维护一个良好氛围的中国技术社区。 欢迎举报不良帖子,欢迎提供建议。
我们的QQ群:136102234,欢迎进群找答案,里边会有很多资深的大师们。 point overflow

常见的很多地方都会用到args和*args。

这两个有什么区别?

asked 2016-05-20 10:10:52

jkant's gravatar image

jkant ♦♦
11133138139
accept rate: 3%


当函数的参数不确定时,可以使用*args 和kwargs,*args 没有key值,kwargs有key值。

link

answered 2016-05-20 10:11:01

jkant's gravatar image

jkant ♦♦
11133138139
accept rate: 3%

例子: def fun_var_args(farg, *args):
print "arg:", farg
for value in args:
print "another arg:", value

fun_var_args(1, "two", 3) # *args可以当作可容纳多个变量组成的list

link

answered 2016-05-20 10:11:28

jkant's gravatar image

jkant ♦♦
11133138139
accept rate: 3%

已编辑 2016-05-20 10:12:37

def fun_var_kwargs(farg, **kwargs):
print "arg:", farg
for key in kwargs:
print "another keyword arg: %s: %s" % (key, kwargs[key])

fun_var_kwargs(farg=1, myarg2="two", myarg3=3) # myarg2和myarg3被视为key, 感觉**kwargs可以当作容纳多个key和value的dictionary

link

answered 2016-05-20 10:12:17

jkant's gravatar image

jkant ♦♦
11133138139
accept rate: 3%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×10
×1

Asked: 2016-05-20 10:10:52

Seen: 1,526 times

Last updated: 2016-05-20 10:12:37