Python:在方法之间传递变量时,是否需要为其指定新名称?

发布于 2024-09-02 10:40:52 字数 177 浏览 3 评论 0原文

我想如果程序很小并且有很多方法,答案可能是“不”,但是在更大的程序中呢?如果我要在整个程序的多个方法中使用一个变量,那么更明智的做法是:

为每个方法使用不同的措辞(以消除命名冲突)。 为每个方法使用相同的名称(以消除混淆) 只需使用全局变量(以消除两者)

这更多的是一个风格问题。传递变量时使用什么命名约定?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

·深蓝 2024-09-09 10:40:52

在方法之间传递变量时......不存在命名冲突,因为变量在每个方法中都是本地的,因此两个方法中的相同名称可以消除混淆。

def printIt(num):
    print(num)

def f():
    num = 2
    printIt(num)

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.

def printIt(num):
    print(num)

def f():
    num = 2
    printIt(num)
丶情人眼里出诗心の 2024-09-09 10:40:52

我倾向于在局部函数中多次重用变量名,只要它们是在进程中的。通常是数据库查找或计算的“结果”,用于中介,并且需要生成或构造方法的最终结果。

我很少使用全局变量,事实上只有在必须时才使用。

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.

风月客 2024-09-09 10:40:52

Python 的作用域规则使您几乎无需为了满足语言需求而对变量使用不同或相同的名称。

变量名称的选择应主要由可读性和可维护性问题驱动。一般来说,最好在整个代码中仅使用一个名称来标识特定类型的事物。这种一致性对读者很有帮助。命名变量、方法和类是您向自己和他人明确意图的最重要工具之一。

有大量(且不完全一致)关于命名事物的文献。需要考虑以下几点:

  1. 范围较小的名称可以是短名称,这些名称不一定能说明变量的全部情况,因为变量的整个上下文很容易可见。 i,j,k 作为循环中的计数器可能是经典的例子——但是在 Python 中你看不到那么多的 for item in collection 循环结构。
  2. 另一方面,具有较大范围的名称(可能是类中的实例变量)应该具有更完整的描述性名称,因为它们出现在初始化/修改上下文不可见的地方。
  3. 尽量不要在你的名字中加入“噪音”。 FrequencyInfo——什么,Info部分到底是添加什么东西的?
  4. 不要在名称中包含数据结构类型——urldict 中的字典并没有多大帮助。
  5. 尽可能使用您正在工作的域中的名称,但不要强迫它。
  6. 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:

  1. Names with small scopes can be short names that do not necessarily tell the whole story about the variable, since the entire context for the variable is easily visible. i,j,k as counters in loops are maybe the classic example---but you don't see that much in Python with its dandy for item in collection loop construct.
  2. The flip side of this is that names with larger scopes (instance variables in a class, maybe) should have more completely descriptive names, since they show up in places where the initialization/modification context is not visible.
  3. Try not to put 'noise' pieces into your names. frequencyInfo---what, exactly does the Info part add to things?
  4. Don't include the data structure type in the name---the dict in urldict doesn't really help you much.
  5. Use names from the domain you are working in when you can, but don't force it.
  6. Python style leans towards terse names. Careful thinking and choices often result in a name that is brief, but apt.
一人独醉 2024-09-09 10:40:52

不要去发明方法来指示该名称属于哪个方法。如果您编码正确,您将不会将其他方法变量视为本地变量。如果你有一个 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.

云仙小弟 2024-09-09 10:40:52

当含义相同时,您应该使用相同的名称。例如,如果您有一个“汽车”数据库,则您可能希望在接受汽车作为参数或在内部使用汽车对象的每个方法或函数中使用名称“汽车”来引用单个汽车。这样做,阅读代码的人会开始对自己说“好吧,这是一辆汽车”,而不是“嗯,我想知道‘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.

2024-09-09 10:40:52

没有这样的要求:在不同的方法或函数参数中为变量赋予不同的名称。事实上,这会极大地损害可读性并且违背常识——当两个事物相同时,人们试图给它们赋予相同的名称,而不是不同的名称。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文