作为函数参数的 Python3 中的作用域:列表。

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

scopes in python3: lists as parameters of a function?

问题

  1. Python knows about my_list_2 within the my_function def block because my_list_2 is a global variable. When the function my_function is called, it has access to global variables within its scope.

  2. Changing the variable name from my_list_2 to pluto results in a NameError because the function my_function refers to a variable my_list_2 that no longer exists. It's unable to find the variable my_list_2 within its scope, hence throwing an error.

  3. In the my_function block, the variable my_list_2 is a global variable and not defined within the function. Therefore, any changes made to my_list_1 (passed as an argument) within the function will affect the original list since lists are mutable objects in Python. Hence, changes to my_list_1 directly impact the same list that my_list_2 is referencing outside the function.

英文:

I'm studying for a python3 exam and came across this question:

Given this snippet of code:

def my_function(my_list_1):
    print("Print #1:", my_list_1)
    print("Print #2:", my_list_2)
    del my_list_1[0]  # Pay attention to this line.
    print("Print #3:", my_list_1)
    print("Print #4:", my_list_2)


my_list_2 = [2, 3]
my_function(my_list_2)
print("Print #5:", my_list_2)

explain the output #3-#5:

Print #1: [2, 3] 
Print #2: [2, 3] 
Print #3: [3] 
Print #4: [3] 
Print #5: [3]

I'm a little perplexed by these questions:

  1. how does python know who 'my_list_2' is, within the 'my_function' def block of code?

  2. Suppose I changed the last 3 lines of code from my_list_2=[2,3] to pluto=[2,3]:

     pluto = [2, 3]
     my_function(pluto)
     print("Print #5:", pluto)
    

this would generate a NameError which I think makes sense -- so why does it not throw an Error in the initial code?

  1. inside the def block, how does Print #4 for my_list_2 reflect the changes applied to my_list_1?

答案1

得分: 1

my_list_2 是一个 global 变量,因此它可以在你的函数中访问。这在 Python 中被称为 LEGB,你可以在 这里 阅读相关信息。

而在 Python 中,list 是一种可变的变量类型。当你将 my_list_2 作为参数传递给你的函数时,如果对它进行任何更改,这将影响你代码的所有部分,因为它是可变的,对它的更改将发生在原地。

英文:

my_list_2 is a global variable. So it's accessible in your function. It's called LEGB in python which you can read about it here .

And list in python is a mutable variable type. When you pass my_list_2 as param to your function, if you make any changes to that, that would apply all over your code. Because it's mutable changes on it would happen in place.

答案2

得分: 0

当你将列表作为参数传递时,解释器会将链接放到对象 "list" 上。

因此,如果你调用

del my_list_1[0]

你修改了你传递的实际列表。因此,my_list_1 和 my_list_2 列表都与相同的对象相关联,你会看到一个元素被删除。

另一种表示的替代代码如下:

my_list_1 = [2, 3]
my_list_2 = my_list_1
del my_list_2[0]
print(my_list_1, my_list_2)
# [3] [3]
英文:

When you put the list as a parameter, the interpreter puts the link to the object "list."

So, if you call

del my_list_1[0]

You modify the actual list you passed. So, as a result, both my_list_1 and my_list_2 lists are related to the same object, and you see one dropped element there.

An alternative code represent that is:

my_list_1 = [2, 3]
my_list_2 = my_list_1
del my_list_2[0]
print(my_list_1, my_list_2)
# [3] [3]

huangapple
  • 本文由 发表于 2023年2月19日 22:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500995.html
匿名

发表评论

匿名网友

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

确定