高手帮 解释下 这段python 代码 50
importtimeran=range(1000)deftimer(func,*pargs,**kargs):start=time.clock()forxinran:li...
import time
ran = range(1000)
def timer(func,*pargs,**kargs):
start = time.clock()
for x in ran:
li = func()
ti = time.clock() - start
return(ti)
def li():
return [x for x in ran]
print(timer(li))
这段代码写成
li = func()
和
li = func(*pargs,**kargs)
得到的结果是一样的
但是有啥区别啊? 展开
ran = range(1000)
def timer(func,*pargs,**kargs):
start = time.clock()
for x in ran:
li = func()
ti = time.clock() - start
return(ti)
def li():
return [x for x in ran]
print(timer(li))
这段代码写成
li = func()
和
li = func(*pargs,**kargs)
得到的结果是一样的
但是有啥区别啊? 展开
4个回答
展开全部
def fun(*arg):
*arg 代表可变个数参数 fun(something,something)
def fun(**arg):
** arg 允许键值对的可变个数参数 调用时fun(arg1 = something,arg2 = something)
看下面、。。。。
How to use *argsand **kwargs in Python
Date: 2008-01-03 | Modified:2012-12-20 | Tags:core,python | 94 Comments
Or,How to use variable length argumentlists in Python.
The special syntax,*argsand**kwargsin function definitions is used to pass a variable numberof arguments to a function. The single asterisk form (*args) is used to pass anon-keyworded, variable-length argument list, and the double asteriskform is used to pass akeyworded, variable-length argument list. Here is an example ofhow to use the non-keyworded form. This example passes one formal (positional)argument, and two more variable length arguments.
deftest_var_args(farg,*args):
print"formalarg:",farg
forarginargs:
print"another arg:",arg
test_var_args(1,"two",3)
Results:
formal arg: 1
another arg: two
another arg: 3
Here is an example of how to use the keyworded form.Again, one formal argument and two keyworded variable arguments are passed.
deftest_var_kwargs(farg,**kwargs):
print"formalarg:",farg
forkeyinkwargs:
print"another keyword arg:%s:%s"%(key,kwargs[key])
test_var_kwargs(farg=1,myarg2="two",myarg3=3)
Results:
formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
Using*argsand**kwargswhencallinga function
This special syntax can be used, not only in functiondefinitions, but also whencallinga function.
deftest_var_args_call(arg1,arg2,arg3):
print"arg1:",arg1
print"arg2:",arg2
print"arg3:",arg3
args=("two",3)
test_var_args_call(1,*args)
Results:
arg1: 1
arg2: two
arg3: 3
Here is an example using the keyworded form when callinga function:
deftest_var_args_call(arg1,arg2,arg3):
print"arg1:",arg1
print"arg2:",arg2
print"arg3:",arg3
kwargs={"arg3":3,"arg2":"two"}
test_var_args_call(1,**kwargs)
Results:
arg1: 1
arg2: two
arg3: 3
*arg 代表可变个数参数 fun(something,something)
def fun(**arg):
** arg 允许键值对的可变个数参数 调用时fun(arg1 = something,arg2 = something)
看下面、。。。。
How to use *argsand **kwargs in Python
Date: 2008-01-03 | Modified:2012-12-20 | Tags:core,python | 94 Comments
Or,How to use variable length argumentlists in Python.
The special syntax,*argsand**kwargsin function definitions is used to pass a variable numberof arguments to a function. The single asterisk form (*args) is used to pass anon-keyworded, variable-length argument list, and the double asteriskform is used to pass akeyworded, variable-length argument list. Here is an example ofhow to use the non-keyworded form. This example passes one formal (positional)argument, and two more variable length arguments.
deftest_var_args(farg,*args):
print"formalarg:",farg
forarginargs:
print"another arg:",arg
test_var_args(1,"two",3)
Results:
formal arg: 1
another arg: two
another arg: 3
Here is an example of how to use the keyworded form.Again, one formal argument and two keyworded variable arguments are passed.
deftest_var_kwargs(farg,**kwargs):
print"formalarg:",farg
forkeyinkwargs:
print"another keyword arg:%s:%s"%(key,kwargs[key])
test_var_kwargs(farg=1,myarg2="two",myarg3=3)
Results:
formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
Using*argsand**kwargswhencallinga function
This special syntax can be used, not only in functiondefinitions, but also whencallinga function.
deftest_var_args_call(arg1,arg2,arg3):
print"arg1:",arg1
print"arg2:",arg2
print"arg3:",arg3
args=("two",3)
test_var_args_call(1,*args)
Results:
arg1: 1
arg2: two
arg3: 3
Here is an example using the keyworded form when callinga function:
deftest_var_args_call(arg1,arg2,arg3):
print"arg1:",arg1
print"arg2:",arg2
print"arg3:",arg3
kwargs={"arg3":3,"arg2":"two"}
test_var_args_call(1,**kwargs)
Results:
arg1: 1
arg2: two
arg3: 3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这所以一样是因为这里的函数 li 不需要参数
即 def li():
你再定义个其他带参数的函数试试, 果断不一样了
即 def li():
你再定义个其他带参数的函数试试, 果断不一样了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
print(timer(li))没有带*pargs,**kargs参数阿,当然li = func()和li = func(*pargs,**kargs)一样了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就是有没有带参数而已,参数是可以省略的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询