TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

*args and **kwargs in python explained

63 点作者 yasoob将近 12 年前

6 条评论

ghc将近 12 年前
I find it interesting to see all of the comments on HN about the dislike of args and kwargs, especially given their necessity for higher order programming.<p>If you are making functions that modify other functions (like decorators), these are highly useful. In Python you can&#x27;t guarantee that all functions you will want to modify are going to have the same type signature, so you need to be able to gracefully handle all possible combinations and pass them on to your function body. Sometimes you&#x27;ll just pass args and kwargs on to a wrapped function...other times you&#x27;ll do things like pick out a single argument from kwargs and use it to do something useful. But make no mistake, in Python these come in handy to have!
评论 #6158317 未加载
rdtsc将近 12 年前
They are also easy to abuse and make APIs hard to figure out. You could see stuff like:<p><pre><code> process_payment_data(**kwargs) </code></pre> In the docs and then have to go find out what possible values kwargs could take.
评论 #6157222 未加载
评论 #6156972 未加载
zanny将近 12 年前
I&#x27;ve always felt like the args and kwargs syntax in python is a blemish on an otherwise beautiful language. I&#x27;m wondering why not just let any function accept variable args, and reserve the keywords args and kwargs (without stars), where in a function body they refer to non-specified &#x2F; explicitly defined arguments?<p>The only downside I see to that is the ability to write code poorly by passing junk arguments into functions that never get used. But you can do that with any function that declares kwargs or args right now.<p>Or just reserve args and kwargs and have it so functions with them in their arguments list have the same behavior as args an kwargs (just without the ugly stars). Maybe even denote them as __args__ and __kwargs__ in the same way you access special member functions of objects through __foo__ syntax, that would at least (to me) be consistent. I did a cursory google search if there is any language wide syntax surrounding star + variable name, and I couldn&#x27;t find any, which seems to mean the name was just a hack using the syntax of a pointer dereference from C.<p>Am I wrong here and just missing some grand logic behind it? The rest of the language is really sensible so I probably am.
评论 #6157175 未加载
评论 #6157344 未加载
评论 #6157431 未加载
评论 #6157150 未加载
评论 #6157239 未加载
mtrn将近 12 年前
After a couple of years with Python I had a first use case for an ordered version of <i></i>kwargs. I haven&#x27;t really dived into the problem yet, but could this be done with a metaclass or is <i></i>kwargs rooted deeper in the stack?
评论 #6157108 未加载
评论 #6156992 未加载
gpsarakis将近 12 年前
I found this article <a href="http://freepythontips.wordpress.com/2013/08/03/a-url-shortener-in-python/" rel="nofollow">http:&#x2F;&#x2F;freepythontips.wordpress.com&#x2F;2013&#x2F;08&#x2F;03&#x2F;a-url-shorten...</a> also interesting and well-written.
tlarkworthy将近 12 年前
that has explained the mechanics of kwargs, but not explained <i>why</i> we would use kwargs. I hardly ever use kwargs because I think they make the source code hard to read.<p>I think they are useful for systems with big configurations (that grow with features during development). I give every customizable function a reference to the big master config dict. Still not sure if that&#x27;s a good design or not ...
评论 #6157197 未加载
评论 #6157098 未加载