英文:
scopes in python3: lists as parameters of a function?
问题
-
Python knows about
my_list_2
within themy_function
def
block becausemy_list_2
is a global variable. When the functionmy_function
is called, it has access to global variables within its scope. -
Changing the variable name from
my_list_2
topluto
results in a NameError because the functionmy_function
refers to a variablemy_list_2
that no longer exists. It's unable to find the variablemy_list_2
within its scope, hence throwing an error. -
In the
my_function
block, the variablemy_list_2
is a global variable and not defined within the function. Therefore, any changes made tomy_list_1
(passed as an argument) within the function will affect the original list since lists are mutable objects in Python. Hence, changes tomy_list_1
directly impact the same list thatmy_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:
-
how does python know who 'my_list_2' is, within the 'my_function'
def
block of code? -
Suppose I changed the last 3 lines of code from
my_list_2=[2,3]
topluto=[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?
- inside the
def
block, how does Print #4 formy_list_2
reflect the changes applied tomy_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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论