python中函数的参数命名约定
这不是一个大问题,但它困扰着我,所以我决定寻求灵感。
假设一个函数定义如下:
def store(book=None, author=None):
pass
当像这样调用这个函数时:
book = Book()
author = Author()
store(book=book, author=author)
我是否必须担心因为 book=book
和 author=author
带来的副作用?我倾向于重新定义该函数
def store(thebook=None, theauthor=None):
pass
,但它似乎有点冗长。有什么建议吗?
This isn't a big deal of a question but it bothers me, so I decided to ask for inspiration.
Assume a function defined like this:
def store(book=None, author=None):
pass
When calling this function like this:
book = Book()
author = Author()
store(book=book, author=author)
do I have to fear sideeffects because of book=book
and author=author
? I am tending to redefine the function to
def store(thebook=None, theauthor=None):
pass
but it seems a little verbose. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无需担心任何副作用。从语义上讲,这与您刚刚调用时没有什么不同。
值存储在函数作用域内的新变量中,并遵守所有正常的 python 作用域规则。
如果需要的话,关键字参数确实可以让您自由地使用其他更具体的变量名称。名称
book
和author
非常通用,适合在函数中使用,该函数应该是可重用的,因此更抽象一些。但在调用范围内使用更具体的名称可能是合适的。从这个角度来看,thebook
和theauthor
与book
和author
没有什么不同;你想做的事情更像是——比如——local_book
或borrowed_book
——或者任何能更准确地描述相关书籍的内容。You have no side effects to fear. It's no different semantically than it would be if you had just called
The values are stored in new variables inside the function's scope, subject to all the normal python scoping rules.
Keyword arguments do free you to use other, more specific variable names if you need to though. The names
book
andauthor
are pretty generic, which is appropriate within a function, which should be reusable and therefore a little bit more abstract. But using more specific names might be appropriate within the calling scope. From that perspective,thebook
andtheauthor
aren't really any different frombook
andauthor
; you'd want to do something more like -- say --local_book
orborrowed_book
-- or whatever would more precisely describe the book in question.首先,
store(book=book,author=author)
没有歧义或副作用。一般来说,解释器可以毫无问题地区分参数名称和名称。现在,关于问题的第二部分,我认为您不应该更改函数参数的名称:毕竟,
store()
确实 执行其工作一般意义上,一本书
和一个作者
。然而,您的局部变量可能更准确地说明它们包含的内容。它们可能不会引用任何书籍或作者,而是具有特定特征的书籍或作者,例如当前书籍或最畅销作者。
因此,如果您希望消除名称歧义,我建议您重命名局部变量。
First, there's no ambiguity or side-effect in saying
store(book=book, author=author)
. The interpreter has no problem telling argument names from names in general.Now, concerning the second part of your question, I don't think you should change the names of the function's arguments: after all,
store()
does perform its work from abook
and anauthor
, in the general sense.Your local variables, however, might be more precise about what they contain. They probably don't reference any book or author, but one having specific characteristics like, say, the current book or the best-selling author.
So, if you wish to disambiguate names, I would suggest you rename your local variables instead.
不,不会有任何副作用。
调用函数中的变量的命名空间和被调用函数中的参数的命名空间是分开的,不能互相影响。
No, there won't be any side effects from that.
The namespace of the variables in the calling function, and the namespace of the parameters within the called function, are separate and can't affect each other.
参数名称与变量参数相同时不会出现任何问题,因为它们的解析方式不同:
来自 http://docs.python.org/reference/grammar.html
You will not get any problem with your parameter names being the same as your variable parameters because they are not parsed the same way:
From http://docs.python.org/reference/grammar.html