简体   繁体   English

Python中的本地和全局列表

[英]Local and Global lists in Python

Please help me as I am new to python when I call this function the original value of list1 changes 当我调用python函数时,请帮帮我,因为list1的原始值发生了变化

def mystery(list1):
  list1[0] , list1[1] = list1[1], list1[0]

list1 = [7,82,44,23,11]
mystery(list1)
print(list1) #prints [82, 7, 44, 23, 11]

how it can change the value of global list1 if I change my function to 如果将函数更改为,它将如何更改全局list1的值

def mystery(list1):
  list1 = list1 + list1[2:5]

then I am getting the global value of list1 and not the updated one. 那么我得到的是list1的全局值,而不是更新的值。

lists are mutable objects in python, so if you are passing to a function a list all the changes that you make to the list inside your function will be reflected everywhere/globally 列表是python中的可变对象,因此,如果要传递给函数列表,则对函数内部的列表所做的所有更改都将在各处/全局反映出来。

If you pass a mutable object into a method, the method gets a reference to that same object and you can mutate it to your heart's delight, but if you rebind the reference in the method, the outer scope will know nothing about it, and after you're done, the outer reference will still point at the original object. 如果将可变对象传递给方法,则该方法将获得对该对象的引用,并且可以对其进行突变,但是如果您将该引用重新绑定到该方法中,则外部作用域对此一无所知完成后,外部参考仍将指向原始对象。

If you pass an immutable object to a method, you still can't rebind the outer reference, and you can't even mutate the object. 如果将不可变对象传递给方法,则仍然无法重新绑定外部引用,甚至无法使对象发生突变。 [more details here] [更多详细信息在这里]

Maybe just return the list? 也许只是返回列表?

def mystery(list1):
    return list1 + list1[2:5]

list1 = [7,82,44,23,11]
list1 = mystery(list1)
print(list1)

The reason list1 is changing is because you're actually modifying the list object inside of the function. 之所以更改list1是因为您实际上是在函数内部修改list对象。

It really has nothing todo with global / local variables, if you renamed the parameter inside your function to something else, and still passed in list1, list1 would be modified. 它确实与全局/局部变量无关,如果您将函数内部的参数重命名为其他名称,并且仍然传递给list1,则list1将被修改。

If you're wanting to return a new list, you need to first create a copy of the list, there's many ways to do this. 如果要返回新列表,则需要首先创建列表的副本,可以通过多种方法进行。 list(list1) is one way. list(list1)是一种方法。 Then return the list at the end of the function. 然后在函数末尾return列表。

If I understand your queston, you want to actually append some more values to the passed in list, use list1.append(...) to add to the end of the list. 如果我了解您的问题,您实际上想将更多值附加到传入列表中,请使用list1.append(...)添加到列表的末尾。

And since you're modifying the list itself, it'll change in the larger scope. 而且,由于您要修改列表本身,因此它将在更大的范围内更改。

But it's still not using the global scope, as you're just using a var with the same name in the local scope. 但是它仍然没有使用全局作用域,因为您只是在本地作用域中使用了具有相同名称的var。

In python, assigning a value to a variable like a = 5 means you are creating an object in the memory for the value 5. Now a is a link to that object holding 5 (in Layman's terms). 在python中,将值分配给像a = 5这样的变量意味着您正在内存中为值5创建一个对象。现在, a是指向该对象的链接,该对象持有5(按Layman的说法)。 If you change a now, say a = "something else" , this creates a new object for the string "something else" in somewhere else in the memory and now a is pointing to that. 如果您现在更改a ,请说a = "something else" ,这将在内存中的"something else"为字符串"something else"创建一个新对象,现在a指向该对象。 This is why you get to change the data type of python variables. 这就是为什么要更改python变量的数据类型的原因。

When you pass something to a python function, that link is the one that is passed on. 当您将某些东西传递给python函数时,该链接就是传递的链接。 So when you call mystery(list1) , original link to the list1 is passed. 因此,当您调用mystery(list1) ,将传递到list1原始链接。 Therefore changing an element in the list1 means you are assigning a new value to that particular element in the original list1 . 因此,更改list1的元素意味着您要为原始list1中的特定元素分配一个新值。 No matter you are inside or outside of the function mystery() in this case since you will be using the original link you created to access the element inside list1 to change it. 在这种情况下,无论您位于函数mystery()内部还是外部,都将使用创建的原始链接访问list1内的元素来更改它。 The change happens inside the list1 ; 更改发生在list1内部; list1 didn't get reassigned. list1没有重新分配。

However when you do list1 = "something new" inside your function mystery() , you are creating a new variable list1 inside the function which is local to the function. 但是,当您在函数mystery()执行list1 = "something new" ,您将在函数内部创建一个新变量list1 ,该变量对于该函数而言是本地的。 You are not changing the original list1 . 您没有更改原始list1

Passing mutable objects into a function and then modifying the object inside the function will have the same effect as modifying the object directly. 将可变对象传递给函数,然后在函数内部修改对象将具有与直接修改对象相同的效果。

list1 = [1,2,3]

def func(list1):
    list1[0] = 5

>>>list1
[5,2,3]

This is effectively same as directly running list1[0] = 5 这实际上与直接运行list1[0] = 5

However if you pass an immutable object into a function such as tuple, It will not support item assignement TypeError: 'tuple' object does not support item assignment . 但是,如果将不可变对象传递给诸如tuple之类的函数,它将不支持项目分配TypeError: 'tuple' object does not support item assignment So you need to build a new immutable object and return it. 因此,您需要构建一个新的不可变对象并返回它。

>>> tup1 = (1,2,3) # doing tup1[0] = 5 will cause TypeError
>>> tup2 = tup1 + (5,)
>>> tup2
(1, 2, 3, 5)

Put it in function, 投入功能

>>> def func2(tup1):
        return tup1 + (5,)

>>> func2(tup1=(1,2,3))
(1, 2, 3, 5)

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

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