问题描述
大家好,
我只是在学习Python并遇到了一些我觉得可能会发生的事情。
是一个bug。请启发我...以下代码提出了
挑战。你怎么在世界上为* arg4提供论据?
## ======================= =========================== ==========
def argPrecedence(par1, par2 = 0,par3 = 0,* par4,** par5):
打印''par1 ='',par1,''#position argument''
打印''par2 ='',par2,''#keyword argument''
print''par3 ='',par3,''#keyword argument''
print ''par4 ='',par4,''#参数转换为元组''
打印''par5 ='',par5,''#参数转换为字典''
argPrecedence(''arg1'',arg3 =''arg3'',arg2 =''arg2'',arg5 =''arg5'')
#argPrecedence结果:
par1 = arg1 #position argument
par2 = arg2#keyword argument
par3 = arg3#keyword argument
par4 =()#参数转换为元组
par5 = {''arg5'':''arg5' '}#参数转换为字典
## ================================= ================= ==========
以上代码含有评论,因为我只是学习
Python和我正在创建我自己的示例和参考资料......你能解决这个问题吗?如果是这样,你能分享解决方案吗?如果没有,这是一个错误,限制还是一个功能?
感谢您抽出宝贵时间!
PS。我可以远离这个代码目的;尝试创建一个
示例,它将在一个单独的def语句中包含所有不同的可能参数
(位置,关键字,*和**)。
-
最好的问候
Victor B. Gonzalez
我只是在学习Python并遇到了一些我觉得可能会发生的事情。
是一个bug。请启发我...以下代码提出了
挑战。你怎么在世界上为* arg4提供论据?
## ======================= =========================== ==========
def argPrecedence(par1, par2 = 0,par3 = 0,* par4,** par5):
打印''par1 ='',par1,''#position argument''
打印''par2 ='',par2,''#keyword argument''
print''par3 ='',par3,''#keyword argument''
print ''par4 ='',par4,''#参数转换为元组''
打印''par5 ='',par5,''#参数转换为字典''
argPrecedence(''arg1'',par3 =''arg3'',par2 =''arg2'',arg5 =''arg5'')
#argPrecedence结果:
par1 = arg1 #position argument
par2 = arg2#keyword argument
par3 = arg3#keyword argument
par4 =()#参数转换为元组
par5 = {''arg5'':'' arg5''}#参数转换为字典
## =============================== =================== ==========
上面的代码很详细,有评论,因为我我正在学习
Python并且正在创建我自己的示例和参考资料......你能解决这个问题吗?如果是这样,你能分享解决方案吗?如果没有,这是一个错误,限制还是一个功能?
感谢您抽出宝贵时间!
PS。我可以远离这个代码目的;尝试创建一个
示例,它将在一个单独的def语句中包含所有不同的可能参数
(位置,关键字,*和**)。
-
最好的问候
Victor B. Gonzalez
不确定你认为这个bug是什么,但我怀疑你'完全
错过了*和**做的点,以及它们如何在实际代码中使用。
< / F>
Hello all,
I am just learning Python and have come across something I feel might
be a bug. Please enlightenment me... The following code presents a
challenge. How in the world do you provide an argument for *arg4?
## ================================================== ==========
def argPrecedence(par1, par2=0, par3=0, *par4, **par5):
print ''par1 ='', par1, '' # positional argument''
print ''par2 ='', par2, '' # keyword argument''
print ''par3 ='', par3, '' # keyword argument''
print ''par4 ='', par4, '' # argument converted to tuple''
print ''par5 ='', par5, '' # argument converted to dictionary''
argPrecedence(''arg1'', arg3=''arg3'', arg2=''arg2'', arg5=''arg5'')
# argPrecedence Results:
par1 = arg1 # positional argument
par2 = arg2 # keyword argument
par3 = arg3 # keyword argument
par4 = () # argument converted to tuple
par5 = {''arg5'': ''arg5''} # argument converted to dictionary
## ================================================== ==========
The code above is verbose with comments because I am just learning
Python and am creating my own examples and reference materials... Can
you solve the problem? If so, can you share the solution? If not, is
this a bug, limitation or is it a feature?
Thank you for your time on this one!
PS. I could be far off with this codes purpose; to try and create an
example that will house all the different possible parameters
(positional, keyword, * and **) in one single def statement.
--
Best Regards
Victor B. Gonzalez
not sure what you think the bug is, but I suspect that you''ve completely
missed the point of what * and ** do, and how they''re used in real code.
</F>
这篇关于参数优先级(可能的错误?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!