避免阴影变量的pythonic方法是什么? [英] What is the pythonic way to avoid shadowing variables?

查看:39
本文介绍了避免阴影变量的pythonic方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常有以下代码会导致变量阴影或局部变量的乘法

I often have the following code which either leads to variable shadowing or to a multiplication of local variables

def whenadult(age):
    return 18 - age

age = 5
needtowait = whenadult(age)

age 在传递给函数时与在主代码中具有相同的逻辑角色,因此我想避免在 whenadult 中创建类似 l_age 的东西.

age has the same logical role both when passed to the function as in the main code so I would like to avoid creating something like l_age in whenadult.

解决阴影与变量乘法"困境的pythonic方法是什么?

What is the pythonic way to solve the "shadowing vs. variable multiplication" dilemma?

更新:跟进一些评论,我想明确表示我正在寻找 Python 最佳实践(而不是局部变量与全局变量作用域)

UPDATE: following up on some comments I want to make it clear that I was looking for a Python best practice (as opposed to local vs. global variables scope)

推荐答案

局部变量(和函数参数)age 恰好与程序中其他地方的变量同名无关紧要.局部变量的全部意义在于它们只存在于定义它们的函数的局部范围内.

The fact that the local variable (and function parameter) age happens to have the same name as a variable somewhere else in your program is irrelevant. The whole point of local variables is that they only live within the local scope of the function they're defined in.

局部变量与其他地方用作参数的变量同名这一事实尤其不是问题.事实上,它在现实生活中的代码中很常见.例如,选择一个随机的 stdlib 模块,cmd 的 3.3 版本,Cmd.onecmd 方法有一个名为 line 的变量,它将它作为参数传递给 self.default方法,它将它绑定到一个名为 line 的参数.

The fact that the local variable has the same name as the variable used elsewhere as an argument is especially not a problem. In fact, it's very common in real-life code. For example, picking a random stdlib module, the 3.3 version of cmd, the Cmd.onecmd method has a variable named line, and it passes it as an argument to the self.default method, which binds it to a parameter that's also named line.

用于参数的变量恰好是一个全局变量,如果您没有同名的局部变量,则本可以访问这一事实,这不是问题,除非您实际上想访问该全局变量.您不想在现有代码中使用,并且几乎不应该 想要.在这种情况下,在大多数现实世界的情况下,这只是一个巧合,没有任何意义,也没有任何影响,不是您必须解决的问题.

The fact that the variable used for the argument happens to be a global variable that you could have accessed, if you didn't have a local variable of the same name, is not a problem unless you actually wanted to access that global variable. Which you didn't want to in your existing code, and almost never should want to. In this case, and in most real-world cases, it's simply a coincidence that means nothing and affects nothing, not a problem you have to solve.

您遇到的问题是 PyCharm 无法猜测您是否希望在 whenadult 中可以访问全局 age.是否有可能(如果不是在这种微不足道的情况下,也许在更复杂的情况下)人类可能会同样感到困惑,从而减慢他对您的代码的理解?或者有一天您必须在某个环境中编写代码,您的代码审查员或老师或其他任何人会拒绝您的代码,因为它没有在没有警告的情况下通过一些 linter?也许吧.

The problem you're having is that PyCharm can't guess whether you wanted the global age to be accessible in whenadult. Is it possible (if not in this trivial case, maybe in more complex cases) that a human might be similarly confused, slowing down his comprehension of your code? Or that you'll one day have to write code in some environment where your code reviewers or teacher or whatever will reject your code because it doesn't pass some linter with no warnings? Maybe.

但实际上,在任何这样的环境中,他们可能首先会抱怨您使用全局变量.你真的不需要在这里.age 是全局的唯一原因是它必须可供顶级代码访问.如果将该代码移动到函数中,age 可以成为该函数中的局部变量.例如:

But really, in any such environment, they'd probably complain about you using global variables in the first place. And you really don't need to here. The only reason age is a global is that it has to be accessible to the top-level code. If you move that code into a function, age can become a local in that function. For example:

def whenadult(age):
    return 18 - age

def main():
    age = 5
    needtowait = whenadult(age)

main() # possibly with an if __name__ == '__main__' guard

这将使 PyCharm 和任何 linter 工具以及任何容易混淆或思维僵化的人类读者感到高兴.它甚至会让你的代码更快一点.另一方面,它需要阅读更多的代码——只有三行和一个缩进,但整个程序只有八行长.因此,您可以根据具体情况进行权衡.

This will make PyCharm happy, and any linter tools, and any easily-confused or rigidly-minded human readers. It'll even make your code a tiny bit faster. On the other hand, it's more code to read—only three lines and one indent, but then the whole program is only eight lines long. So, it's a tradeoff that you can make on a case-by-case basis.

这篇关于避免阴影变量的pythonic方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆