简体   繁体   English

当我在函数中传递一个完全不同的参数名称时会发生什么?

[英]What happens when I pass a totally different argument name in a function?

Let's say I have this function: 假设我有这个功能:

def print_a_line(line_count, f):
    print line_count, f.readline()

I call it by: 我叫它:

current_line = 3
print_a_line(current_line, current_file)

What happens now? 现在发生了什么? line_count = 3 or will it be line_count= current_line = 3? line_count = 3还是line_count = current_line = 3?

Also, is it a bad thing if I pass different argument names(like I did here with current_line instead of line_count?) 另外,如果我传递不同的参数名称(比如我在这里使用current_line而不是line_count?),这是一件坏事吗?

What happens now? 现在发生了什么? line_count = 3 or will it be line_count= current_line = 3? line_count = 3还是line_count = current_line = 3?

line_count is bound to 3 inside the call to print_a_line . line_countprint_a_line的调用中绑定到3。 The variable current_line is unaffected by the call. 变量current_line不受调用的影响。

Also, is it a bad thing if I pass different argument names(like I did here with current_line instead of line_count?) 另外,如果我传递不同的参数名称(比如我在这里使用current_line而不是line_count?),这是一件坏事吗?

It's not a bad thing at all. 这根本不是坏事。 You are using positional arguments rather than named arguments. 您正在使用位置参数而不是命名参数。

In Python, there are two kinds of arguments: positional arguments and named arguments. 在Python中,有两种参数:位置参数和命名参数。 A function declared like your print_a_line function above is using only positional arguments. 声明为上面的print_a_line函数的函数仅使用位置参数。 A function with only named arguments might look like 只有命名参数的函数可能看起来像

def print_a_line_2(line_count=3, f=None):
    if f is not None:
        print line_count, f.readline()

A function with both positional and named arguments might look like 具有位置和命名参数的函数可能看起来像

def print_a_line_2(line_count, f, append_newline=True):
    if append_newline:
        print line_count, f.readline()
    else:
        print line_count, f.readline(),

The reason that positional arguments are called positional is that only the position of the arguments you pass matters. 位置参数被称为位置的原因是只有你传递的参数的位置才重要。 So you can write any two expression you like as the arguments to print_a_line , and whichever argument is passed first will be bound to line_count , and whichever argument is passed second will be bound to f during the execution of print_a_line . 因此,您可以将您喜欢的任何两个表达式作为print_a_line的参数print_a_line ,并且首先传递的参数将绑定到line_count ,并且在执行print_a_line期间,第二个传递的参数将绑定到f

That's not the case for named arguments , but the syntax is different there. 对于命名参数 ,情况并非如此,但语法不同。 To pass a named argument, you write name=expression instead of simply writing expression . 要传递命名参数,可以编写name=expression而不是简单地编写expression So, to call print_a_line_2 , you could write 所以,要调用print_a_line_2 ,你可以写

print_a_line_2(line_count=3, f=current_file)

and the names of the arguments come from what is before the equals sign, not what is after the equals sign. 并且参数的名称来自等号之前的内容,而不是等号之后的内容。

For more on named and positional arguments, I would recommend checking out the Python tutorial . 有关命名和位置参数的更多信息,我建议您查看Python教程

EDIT : What happens under the hood whenever you call line_count(current_line, current_file) 编辑 :每当你调用line_count(current_line, current_file)时会发生什么

The Python interpreter maintains several pieces of information about your program, and some of the important things it maintains are a symbol table, which binds names (variables) to their values, and a current statement pointer. Python解释器维护有关程序的若干信息,它维护的一些重要事项是符号表,它将名称(变量)绑定到它们的值,以及当前语句指针。 Whenever the current statement pointer reaches the line 每当当前语句指针到达该行时

print_a_line(current_line, current_file)

the Python interpreter looks up print_a_line , current_line , and current_file in the symbol table. Python解释器在符号表中查找print_a_linecurrent_linecurrent_file It finds that print_a_line is bound to the function that you defined in your question, that current_line is bound to 3, and that current_file is bound to a file object (this is a big implementation-defined data structure, which for ease of notation I'll call F, with the uppercase F distinguished from the lowercase f we will meet in a bit). 它发现print_a_line绑定到您在问题中定义的函数, current_line绑定到3,并且current_file绑定到文件对象(这是一个大的实现定义的数据结构,为了便于表示我' ll调用F,大写字母F区别于小写字母f我们将稍微相遇)。 Since print_a_line is a function, the interpreter calls the function with arguments 3 and F. To accomplish this, it saves the current state of execution, binds variable line_count to 3 and f to F in the symbol table, and moves the current statement pointer to the first line of the print_a_line function, which is 由于print_a_line是一个函数,解释器使用参数3和F调用函数。为此,它保存当前的执行状态,将变量line_count绑定到符号表中的3和f到F,并将当前语句指针移动到print_a_line函数的第一行,即

print line_count, f.readline()

It then executes the built-in print statement in much the same way as it executed the original function call, looking up all variables in the symbol table and making a function call to f.readline() in the same way as the overall function call to print_a_line . 然后它执行内置的print语句的方式与执行原始函数调用的方式非常相似,查找符号表中的所有变量,并以与整个函数调用相同的方式调用f.readline()函数。 to print_a_line Then, when the print_a_line function returns, the Python interpreter removes line_count and f from the symbol table, and moves the statement pointer back to the location it saved earlier. 然后,当print_a_line函数返回时,Python解释器从符号表中删除line_countf ,并将语句指针移回到之前保存的位置。 It then continues execution with the line of code after the call to print_a_line . 然后在调用print_a_line之后继续执行代码行。

You're confusing scopes. 你混淆了范围。 Let's assume your file looks like this: 我们假设您的文件如下所示:

def print_a_line(line_count, f):
    print line_count, f.readline()

def main():
    current_line = 3
    print_a_line(current_line, current_file)

if __name__ == "__main__":
    current_file = open('file.txt')
    main()

In main , you have current_line=3 , and current_line is a local variable. main ,你有current_line=3current_line是一个局部变量。 The method print_a_line and the variable current_file are global, which is why main can use them. 方法print_a_line和变量current_file是全局的,这就是main可以使用它们的原因。 In print_a_line , you have the local variables line_count and f , which are the same as current_line and current_file , respectively. print_a_line ,您有局部变量line_countf ,它们分别与current_linecurrent_file相同。 But, because current_line is local to main and line_count is local to print_a_line , there is no scope where current_line == line_count would evaluate to True . 但是,因为current_linemain本地,而line_countprint_a_line本地,所以current_line == line_count没有将其评估为True

It is definitely not a bad thing to do this - the same value might mean different things in different scopes, and the names of the variables should reflect this. 这样做绝对不是一件坏事 - 相同的值可能意味着不同范围内的不同事物,变量的名称应该反映这一点。

Read up in the places the others have recommended. 阅读其他人推荐的地方。 Using different names when you define and when you call a function is not a bad thing; 在定义和调用函数时使用不同的名称并不是一件坏事; in fact I would recommend it until you have a good grasp of scopes and all that. 事实上,我会推荐它,直到你掌握了范围和所有这些。

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

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