为什么python中的列表操作会在函数作用域之外进行? [英] Why do list operations in python operate outside of the function scope?

查看:35
本文介绍了为什么python中的列表操作会在函数作用域之外进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的python代码中,变量number被传递给函数addone,并对本地副本进行操作.number 的值保持不变.

def addone(num):数量 = 数量 + 1打印功能:添加 1,现在 %d" % num数字 = 5打印之前:",数字addone(号码)打印之后:",数字

输出:

之前:5功能:增加1个,现在6个之后:5

然而,行为似乎与弹出、追加等列表操作不同.这让我有些困惑.所有列表操作都在全球范围内运行吗?如果有,背后有什么特别的原因吗?

def pop_first(stuff):弹出 = 东西.pop(0)打印函数:'%s' 被弹出!"% 弹出words = ["A", "list", "of", "words"]打印之前:",单词pop_first(单词)打印之后:",单词

输出:

之前:['A', 'list', 'of', 'words']功能:'A'被弹出!之后:['list', 'of', 'words']

解决方案

简短的回答是因为列表是可变的,而整数是不可变的.

<小时>

你不能就地改变一个整数,所以我们称它为不可变的".考虑到这一点,诸如整数加法之类的事情不会修改原始对象,而是返回一个新值 - 因此您的原始变量将保持不变.因此,如果我们存储对整数的引用,只要我们没有更改其中任何一个,它们就只会是同一个对象:

<预><代码>>>>富 = 1>>>酒吧 = foo>>>foo 是酒吧真的>>>富+= 23>>>富3>>>酒吧1>>>foo 是酒吧错误的

<小时>

另一方面,列表是可变的"(可以修改相同的对象引用),而像 pop() 这样的操作会就地改变 list,改变原来的.这也意味着,如果您编辑对可变对象(例如 list)的引用,原始对象也会更改:

<预><代码>>>>巴兹 = [1, 2, 3, 4, 5]>>>qux = 巴兹>>>qux 是 baz真的>>>baz.pop()5>>>qux[1, 2, 3, 4]>>>巴兹[1, 2, 3, 4]>>>qux 是 baz真的

In the python code below, variable number is passed to the function addone, and a local copy is operated on. The value of number stays the same.

def addone(num):
    num = num + 1
    print "function: added 1, now %d" % num

number = 5
print "Before:", number
addone(number)
print "After:", number

Output :

Before: 5
function: added 1, now 6
After: 5

However, the behavior appears to be different with list operations like pop, append etc. This somewhat confuses me. Do all list operations operate globally ? If so, is there any particular reason behind it ?

def pop_first(stuff):
    popped = stuff.pop(0)
    print "function: '%s' was popped!" % popped

words = ["A", "list", "of", "words"]
print "Before:", words
pop_first(words)
print "After:", words

Output :

Before: ['A', 'list', 'of', 'words']
function: 'A' was popped!
After: ['list', 'of', 'words']

解决方案

The short answer is because lists are mutable and integers are immutable.


You cannot mutate an integer in place, so we call it 'immutable'. With this in mind, things like addition on an integer do not modify the original object, but rather return a new value- so your original variable will remain the same. Therefore, if we store a reference to an integer, they will only be the same object as long as we have not changed either one of them:

>>> foo = 1
>>> bar = foo
>>> foo is bar
True
>>> foo += 2
3
>>> foo
3
>>> bar
1
>>> foo is bar
False


On the other hand lists are 'mutable' (can modify that same object reference), and operations like pop() mutate the list in-place, changing the original. This also means that if you edit a reference to a mutable object such as a list, the original will be changed as well:

>>> baz = [1, 2, 3, 4, 5]
>>> qux = baz
>>> qux is baz
True
>>> baz.pop()
5
>>> qux
[1, 2, 3, 4]
>>> baz
[1, 2, 3, 4]
>>> qux is baz
True

这篇关于为什么python中的列表操作会在函数作用域之外进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆