功能参数**?这些是什么意思? [英] Function params with **? what do these mean?

查看:93
本文介绍了功能参数**?这些是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉这样一个基本问题,但是我无法用短语

a搜索给我一个答案而且我的书完全沉默了

这个。我已经看到了一些python函数defs,它们采用了形式(** param1)的
参数。看起来像一个指针...但是我的

关于python的书籍(基本上都是这样)并没有提及。什么是

这个?


杰夫

I''m sorry for such a basic question, but I haven''t been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don''t make a mention. What is
this?

Jeff

推荐答案

2006年3月20日12:46:43 -0800在comp.lang.python中,J Rice

< ri ********** @ gmail.com>写道:
On 20 Mar 2006 12:46:43 -0800 in comp.lang.python, "J Rice"
<ri**********@gmail.com> wrote:
我很抱歉这样一个基本问题,但是我还没能说出一个搜索,它给我一个答案,我的书是这完全沉默了。我已经看到了一些python函数defs,它们采用了形式的参数(** param1)。看起来像一个指针......但是我的关于python的书籍(基本上都是这样)并没有提及。什么是
这个?
I''m sorry for such a basic question, but I haven''t been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don''t make a mention. What is
this?




这是一种接受不同数量的命名参数的方法。在

函数中,参数变为一个字典,参数名称为

与传递的参数值对应的键。


解释比理解更难。尝试使用python解释器中的

跟随函数:


def test(a,b =''b'',* c,** d ):

打印a,b,c,d


一些测试建议:


test( 1,2,3,4)

测试(a = 1,b = 2,c = 3,d = 4)

test(2,4,6, 8,10,12,ralph = 23,tony = 45)


看看会发生什么。应该大部分都是不言自明的。


问候,

- =戴夫


-

改变是不可避免的,进步不是。



It''s a way of accepting a varying number of named arguments. In the
function, the parameter becomes a dictionary with parameter names as
the keys corresponding to the passed parameter values.

It''s harder to explain than understand. Try playing with the
following function in the python interpreter:

def test(a,b=''b'', *c, **d):
print a,b,c,d

A couple suggestions for tests:

test(1,2,3,4)
test(a=1,b=2,c=3,d=4)
test(2,4,6,8,10,12,ralph=23,tony=45)

See what happens. Should be mostly self-explanatory.

Regards,
-=Dave

--
Change is inevitable, progress is not.


在参数列表中,** param得到一个参数的字典,而不是
对应于正式参数列表中的内容。


更多& python文档中的示例:
http:// docs。 python.org/tut/node6.htm...00000000000000


-

Jordan T. Greenberg
in the parameter list, **param gets a dict of arguments that dont
correspond to somthing in the formal parameter list.

More & examples in the python docs:
http://docs.python.org/tut/node6.htm...00000000000000

--
Jordan T. Greenberg


J Rice写道:
我很抱歉这个基本问题,但我还没能说出
a搜索让我得到答案,我的书完全沉默于
这个。我已经看到了一些python函数defs,它们采用了形式的参数(** param1)。看起来像一个指针......但是我的关于python的书籍(基本上都是这样)并没有提及。什么是
这个?

杰夫
I''m sorry for such a basic question, but I haven''t been able to phrase
a search that gets me an answer and my books are totally silent on
this. I have seen a number of python function defs that take
parameters of the form (**param1). Looks like a pointer... but my
books on python (basic as they are) don''t make a mention. What is
this?

Jeff



有太多表格可能让你感到困惑。首先


There are too forms that you may be confusing. First

def foo(x,y,z):
返回x + y + z
t = [1,2,3]
foo(* t)


$

这告诉python扩展t并传递它至于3

单独的论点


第二个是:

def foo(* args):
返回总和(args)
foo(1,2,3)





这允许你处理所有的参数函数

作为参数列表,无论有多少。




def bar(** kwargs):
对于密钥,值为kwargs.items():
print" key =%s,value =%s" %(键,值)
返回
bar(a = 1,b = 2,c =这是一个测试)
def foo(x,y,z):
return x+y+z t=[1,2,3] foo(*t)
6
This tells python to expand t and pass it as as 3
separate arguments

The second is:
def foo(*args):
return sum(args) foo(1,2,3)
6

This allows you to treat all the arguments to a function
as a list of arguments no matter how many there are.

or
def bar(**kwargs):
for key, value in kwargs.items():
print "key=%s, value=%s" % (key, value)
return bar(a=1, b=2, c="this is a test")



key = a,value = 1

key = c,value =这是一个测试

key = b,value = 2

这允许你通过字典中的字典

来访问关键字参数,而不是单独访问。

你可以将它们结合起来制作非常强大的函数/方法

可以处理不同数量的参数和关键字

参数。


def foo(* args,** kwargs):

....

拉里贝茨


key=a, value=1
key=c, value=this is a test
key=b, value=2
This allows you to access keyword arguments via a dictionary
in the function, instead of individually.
You can combine these to make very powerful functions/methods
that can handle different numbers of arguments and keyword
arguments.

def foo(*args, **kwargs):
....
Larry Bates


这篇关于功能参数**?这些是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆