简体   繁体   English

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

[英]Naming practice for optional argument in python function

Is it OK to give the optional argument in a python function the same name as an outside variable? 将python函数中的可选参数赋予与外部变量相同的名称是否可以? It seems to work fine as functions f and g defined below give the same output. 由于下面定义的函数f和g给出相同的输出,因此似乎工作正常。 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

It will work, allthough 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. 它将起作用,因为主命名空间中的var1与函数命名空间中的var1是完全不同的变量。 And the call of somefunction will work as shown, even if you -seemingly- use var2 twice in one line. 而且,即使您似乎在一行中两次使用了var2,对某些函数的调用也将按所示工作。 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 Pythons main advantages over some other languages is it's readability, you should support that through your coding style. 与其他某些语言相比,Python的主要优点之一是它的可读性,您应该通过自己的编码风格来支持它。

And naming variables a and other variables A is something you should avoid too. 命名变量a和其他变量A也应该避免。 Have a read about naming conventions in pyhton here: www.python.org/doc/essays/styleguide.html and a read about namespaces here: http://bytebaker.com/2008/07/30/python-namespaces/ 在此处阅读有关pyhton中的命名约定的信息:www.python.org/doc/essays/styleguide.html,并在此处阅读有关名称空间的信息: http ://bytebaker.com/2008/07/30/python-namespaces/

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. 另一半正在编写清晰明了的代码,以便您(或其他人)在一年后可以理解它。

Stop to use the word "variable" in Python ! 停止在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 ! 高兴!

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. 但是最好的命名方法是不将函数和变量称为a,f,g,x。 Then you're unlikely to get a collision. 那么您就不太可能发生碰撞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM