python函数中可选参数的命名实践

发布于 2024-10-24 05:47:46 字数 235 浏览 1 评论 0 原文

可以给 python 函数中的可选参数指定与外部变量相同的名称吗? 它似乎工作得很好,因为下面定义的函数 f 和 g 给出了相同的输出。然而,这是否被认为是不好的做法,并且在某些情况下会失败吗?

a = 1
def f(x,A=a): return x*A
def g(x,a=a): return x*a

print g(2)
>> 2
print g(2,a=2)
>> 4

Is it OK to give the optional argument in a python function the same name as an outside variable?
It seems to work fine as functions f and g defined below give the same output. However, is this considered bad practice and does it fail in some cases?

a = 1
def f(x,A=a): return x*A
def g(x,a=a): return x*a

print g(2)
>> 2
print g(2,a=2)
>> 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

毁虫ゝ 2024-10-31 05:47:46

它会起作用,尽管它很容易有点令人困惑,但它是完全有效的。这是因为函数和函数调用发生在不同的命名空间中。因此,如果您编写:

def some_function(var1,var2=somevalue):  #var1 & var2 of functions namespace
    some things happening here

var1 = somevalue #mainspace var1
var2 = somevalue #mainspace var2
some_function(var1,var2=var2) 
#upper line: first var2 = from function, second var2 = from mainspace 

它将起作用,因为主命名空间中的 var1 与函数命名空间中的 var1 是完全不同的变量。即使您看似在一行中使用了 var2 两次,some_function 的调用也将按所示方式工作。但正如你在解释时的困难中看到的那样,这样做会产生一些混乱,所以如果可以的话最好跳过这样的事情。 Python 相对于其他语言的主要优势之一是它的可读性,您应该通过您的编码风格来支持这一点。

命名变量 a 和其他变量 A 也是您应该避免的。阅读有关 python 中的命名约定 在这里

It will work, although it can easily be a bit confusing, it is perfectly valid. This is due to the fact that the function and the call to the function happen in different namespaces. So if you write:

def some_function(var1,var2=somevalue):  #var1 & var2 of functions namespace
    some things happening here

var1 = somevalue #mainspace var1
var2 = somevalue #mainspace var2
some_function(var1,var2=var2) 
#upper line: first var2 = from function, second var2 = from mainspace 

it will work, as var1 in the main namespace is an entirely different variable than var1 in the functions namespace. And the call of some_function will work as shown, even if you -seemingly- use var2 twice in one line. But as you can see in the difficulties while explaining, there is some confusion arised by doing this, so better skip things like that if you can. One of Python's main advantages over some other languages is its readability, you should support that through your coding style.

And naming variables a and other variables A is something you should avoid too. Have a read about naming conventions in python here. and a read about namespaces here.

暗恋未遂 2024-10-31 05:47:46

它会起作用,但我认为很多人会阻止它,因为它很容易令人困惑。

编程工作的一半是获取有效的代码。另一半是编写清晰明了的代码,以便您(或其他人)一年后可以理解它。

It will work, but I think many would discourage it because it can easily be confusing.

Half of the work of programming is getting code that works. The other half is writing code that's clear and obvious, so that you (or someone else) can understand it a year later.

半窗疏影 2024-10-31 05:47:46

停止在 Python 中使用“变量”这个词!

不存在“值可以改变的内存块”意义上的变量

,并且使用“变量”作为“标识符”或“名称”的同义词是令人困惑的;

请啊啊啊!

Stop to use the word "variable" in Python !

There are no variables in the sense of "chunk of memory whose value can change"

And the use of "variable" as synonym of "identifier" or "name" is confusing;

Pleaaaase !

生活了然无味 2024-10-31 05:47:46

如果您想将变量传递给它,它会失败。

但最好的命名实践是函数和变量不称为 a、f、g、x。那么你就不太可能发生碰撞。

it fails if you want to pass the variable to it.

But the best naming practice is one where your functions and variables are not called a,f,g,x. Then you're unlikely to get a collision.

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