Python:在方法之间传递变量时,是否需要为其指定新名称?
我想如果程序很小并且有很多方法,答案可能是“不”,但是在更大的程序中呢?如果我要在整个程序的多个方法中使用一个变量,那么更明智的做法是:
为每个方法使用不同的措辞(以消除命名冲突)。 为每个方法使用相同的名称(以消除混淆) 只需使用全局变量(以消除两者)
这更多的是一个风格问题。传递变量时使用什么命名约定?
I'm thinking that the answer is probably 'no' if the program is small and there are a lot of methods, but what about in a larger program? If I am going to be using one variable in multiple methods throughout the program, is it smarter to:
Come up with a different phrasing for each method (to eliminate naming conflicts).
Use the same name for each method (to eliminate confusion)
Just use a global variable (to eliminate both)
This is more of a stylistic question than anything else. What naming convention do YOU use when passing variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在方法之间传递变量时......不存在命名冲突,因为变量在每个方法中都是本地的,因此两个方法中的相同名称可以消除混淆。
When passing variables between methods ... there are no naming conflicts, since the variable is local in each method, the same name in both methods eliminates confusion.
我倾向于在局部函数中多次重用变量名,只要它们是在进程中的。通常是数据库查找或计算的“结果”,用于中介,并且需要生成或构造方法的最终结果。
我很少使用全局变量,事实上只有在必须时才使用。
I tend to reuse variable names in local functions a lot, as long as they are mid-process. Usually 'result' for a db lookup or a calculation that is used intermediary and is needed to generate or construct the end-result of the method.
Only rarely have I used global variables, in fact only if I must.
Python 的作用域规则使您几乎无需为了满足语言需求而对变量使用不同或相同的名称。
变量名称的选择应主要由可读性和可维护性问题驱动。一般来说,最好在整个代码中仅使用一个名称来标识特定类型的事物。这种一致性对读者很有帮助。命名变量、方法和类是您向自己和他人明确意图的最重要工具之一。
有大量(且不完全一致)关于命名事物的文献。需要考虑以下几点:
Python's scoping rules pretty much free you from the need to use different or identical names for variables just to satisfy language needs.
Variable name choice should be driven primarily by readability and maintainability concerns. In general, it is good to use just one name to identify a particular kind of thing throughout the code. This kind of consistency is helpful to readers. Naming variables, methods, and classes is one of the biggest tools you have to make your intent clear---to both yourself and others.
There is a large (and not entirely consistent) literature about naming things. A couple of things to consider:
不要去发明方法来指示该名称属于哪个方法。如果您编码正确,您将不会将其他方法变量视为本地变量。如果你有一个 foobar 方法的句柄并且想要它的 foo 值,请使用 foobar.foo。
变量名称应指示变量的内容并有助于理解代码。因为很多方法都使用 foos,所以用很多不同的名称来引用 foo 是没有帮助的。您最好花时间来澄清这里的 foo 不是那里的 foo 的情况。对于这些情况,您可能需要在至少一种情况下使用不同的术语。在代码中记录该情况。
对变量使用相同的名称将有助于跟踪您在代码中使用关键对象的位置。您还将拥有许多通用变量,使代码更易于理解:i、j 和 k 用于迭代器;结果为结果为中间结果; ETC。
Don't go inventing ways of indicating which method the name belongs to. If you are coding correctly you will won't be treating other methods variables as local. If you have a hanle on method foobar and want its foo value use foobar.foo.
Variable names should indicate contents of the variable and aid in understanding the code. Referring to a foo by a lot of different names because a lot of methods use foos won't help. Your time may be better spent clarifying cases where a foo here is not a foo there. For those cases you problably need to use different terminology in at least one case. Document the case in your code.
Using the same name for the variable will help tracing where you use key objects in your code. You will also have a lot of generic variables which make code easier to understand: i, j, and k for interators; result for result for intermediate results; etc.
当含义相同时,您应该使用相同的名称。例如,如果您有一个“汽车”数据库,则您可能希望在接受汽车作为参数或在内部使用汽车对象的每个方法或函数中使用名称“汽车”来引用单个汽车。这样做,阅读代码的人会开始对自己说“好吧,这是一辆汽车”,而不是“嗯,我想知道‘foo’是什么类型的东西......”。
因此,使用变量名称时要保持一致,这意味着您可以多次使用相同的名称,只要它具有相同的含义即可。如果可能的话,切勿使用相同的名称来表示两种不同的事物。例如,不要使用“项目”在一个上下文中表示一辆汽车,而在另一个上下文中表示列表中的随机项目。
You should use the same name when it means the same thing. For example, if you have a database of "cars", you might want to use the name "car" to refer to a single car in every method or function that accepts a car as an argument or works with car objects internally. Doing this, someone reading the code will begin to say to themselves "ok, this is a car" rather than "hmmm, I wonder what type of thing 'foo' is ...".
So, be consistent in using variable names, and that means you can use the same name many times as long as it has the same meaning. If at all possible, never use the same name to mean two different things. For example, don't use "item" to mean a car in one context and a random item from a list in another.
没有这样的要求:在不同的方法或函数参数中为变量赋予不同的名称。事实上,这会极大地损害可读性并且违背常识——当两个事物相同时,人们试图给它们赋予相同的名称,而不是不同的名称。
There's no such requirement to give variables different names in different methods or function arguments. In fact, it would greatly hurt readability and go against common sense -- when two things are the same, one tries to give them the same name, not different ones.