区分局部变量和全局变量的常见方法是什么?

huangapple go评论65阅读模式
英文:

What are the common ways to distinguish between identifier of local variables and global variables?

问题

我编写了一个函数,其中使用相同的标识符来表示局部变量和全局变量,如下所示的代码:

```python
def func(time_len, num_nodes):
    pass

if __name__ == '__main__':
    time_len, num_nodes = input_arrs.shape
    func(time_len=time_len, num_nodes=num_nodes)

我想减少这种命名风格以避免歧义。
但我能想到的唯一方法是为标识符添加前缀或后缀,就像这样:

def func(time_len, num_nodes):
    pass

if __name__ == '__main__':
    global_time_len, global_num_nodes = input_arrs.shape
    func(time_len=global_time_len, num_nodes=global_num_nodes)

我的问题是:
有哪些常见的方法或约定来区分局部变量和全局变量?


<details>
<summary>英文:</summary>

I write a function with using same identifier for local variable and globel variable, as shown as following codes:

```python
def func(time_len, num_nodes):
    pass

if __name__ == &#39;__main__&#39;:
    time_len, num_nodes = input_arrs.shape
    func(time_len=time_len, num_nodes=num_nodes)

I want to reduce this kind of naming style to avoid ambiguity.
But the only method I can think of is adding prefix or suffix to identifier, like this:

def func(time_len, num_nodes):
    pass

if __name__ == &#39;__main__&#39;:
    global_time_len, global_num_nodes = input_arrs.shape
    func(time_len=global_time_len, num_nodes=global_num_nodes)

My question is:
What are the common ways or conventions to distinguish between local variables and global variables?

答案1

得分: 1

这不是典型的Python编程风格,来实现你所建议的方式。 https://pep8.org/#global-variable-names

然而,还有其他几种风格可以供你参考。

你可以尝试使用 g_ 前缀来表示全局变量。我曾经见过这种用法,尽管它不是很流行。

一些代码风格还对全局变量提供了指导,其中全局变量(和函数)使用 UpperCamelCase,而局部变量使用 lowerCamelCase。如果你更喜欢使用下划线,可以轻松修改为 Upper_With_Underlower_with_under

通常,在现代的代码风格中,我们不依赖命名来表示变量的作用域。如果程序员无法跟踪哪个名称属于哪个作用域,那应该告诉你文件过于复杂,名称过多,作用域重叠太多。你应该将这种混乱转化为简化、模块化和抽象的一轮过程,以使代码更容易理解并记住。

对你来说难以跟踪的复杂代码是难以修改的代码。

英文:

It is not typical Python Style to do what you suggest. https://pep8.org/#global-variable-names

However, there are several other styles you can take inspiration from.

You can try a g_ prefix to note a global variable. I've seen this used although it is not popular.

Some code styles also have guidance on global variable where you use UpperCamelCase for global variables (and functions) and lowerCamelCase for locals. If you instead prefer underscores, it's easily modified to be Upper_With_Under and lower_with_under.

Typically, in modern code styles we don't rely on naming to note which variable is in what scope. If a programmer cannot keep track of which name is in what scope, that should tell you that the file is too complex with too many names and too many overlapping scopes. You should take this confusion and turn it into a round of simplification, modularization, and abstraction so that the code is easier to understand and keep in your mind.

Code that is too complex for you to keep track of is code that is too complex to modify.

答案2

得分: 1

第一点观察是,如果变量是全局的,你不需要将它们传递给函数;函数已经可以访问它们。然而,最好的策略是尽量避免使用全局变量,这样函数就可以是自包含的。

通常情况下,使用更具描述性的名称来命名全局变量是良好的实践,或者至少避免不必要的缩写,例如elapsed_timenode_count。我总是让最后一个词来描述变量的最基本含义;第一个变量是一个时间,第二个是一个计数。通过这种实践,你可以一眼看出你正在处理什么类型的值。

请记住,每个变量或函数参数都存在于一个上下文中;全局变量存在于模块的上下文中,参数和局部变量存在于函数的上下文中。这通常暗示了名称应该是什么样子。

如果你问我,将全局属性编码到变量名中是一个丑陋的解决方案。这是丑陋的,因为一个变量应该根据它保存的值的类型来命名,不多也不少。

英文:

The first observation is that if the variables are global you don't need to pass them to the function; it already has access to them. However, the best strategy is to avoid global variables if possible so the function is self-contained.

In general it's good practice to use more descriptive names for global variables, or at least avoid unnecessary abbreviations, for instance elapsed_time and node_count. I always let the last word describe the variable in its most basic sense; the first variable is a time and the second is a count. With this practice you can see at a glance what kind of value you are dealing with.

Remember that each variable or function parameter exists in a context; a global variable exist in the context of the module and a parameter and a local variable exist in the context of a function. That alone often implies what the name should be.

Encoding the global property in the variable name is an ugly solution if you ask me. It is ugly because a variable should be named after what kind of values it holds, nothing more and nothing less.

huangapple
  • 本文由 发表于 2023年5月21日 09:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定