Holeinscope,死代码还是为什么这样输出?

2024-05-28 23:36:14 发布

您现在位置:Python中文网/ 问答频道 /正文

编码

def change1(list1):
        list1[1] = list1[1] + 5

def change2(number):
        number = number + 2

def main():
        numbers = [4, 8, 12]
        change1(numbers)
        variable = 15
        change2(variable)
        i = 0
        while i < 3:
                print numbers[i]
                i += 1
        print variable

main()

当我读到它时,我以为它会输出4 8 12 15,但是它输出了4 13 12 15。在这里我可以看到Python处理整数和列表的方式不同,我假设最后一件事没有global是不可能的。我无法理解输出,在这种情况下,为什么它不输出4 13 12 17?在

您可以在这里看到几乎相同的代码,具有不同的类型和不同的引用:

^{pr2}$

通过参考示例

py:通过引用传递和可变数据类型-示例。表/列表不足以影响main中的局部变量,需要引用!在

def change1(list1):
    list1[1] = list1[1] + 5

def change2(number):
    number = [x+2 for x in number]

def main():
    numbers = [4, 8, 12]
    change1(numbers)
    variable = [15]
    change2(variable)
    i = 0
    while i < 3:
        print numbers[i]
        i += 1
    print variable[0]

main()

test3.py:通过引用传递示例,更改main函数外部的可变数据类型list/table

def change1(list1):
    list1[1] = list1[1] + 5

def change2(number):
    number[0] += 2

def main():
    numbers = [4, 8, 12]
    change1(numbers)
    variable = [15]
    change2(variable)
    i = 0
    while i < 3:
        print numbers[i]
        i += 1
    print variable[0]

main()

传递值示例

test4.py:试图找到一个带有pass-by值的示例,为什么它不起作用?在

$ cat test4.py 

# Not yet a pass-by-value example!

global variable
variable = [15]

def change1(list1):
    list1[1] = list1[1] + 5

def change2(number):
    number = [x+2 for x in number] 

def main():
    numbers = [4, 8, 12]
    change1(numbers)
    #variable = 15
    change2(variable)
    i = 0
    while i < 3:
        print numbers[i]
        i += 1
    print variable[0]

main()
$ python test4.py 
4
13
12
15   # I expected 17! Why no 17?

Tags: py示例number列表maindefvariableglobal
3条回答

确切的答案是Python实际上是“按共享调用”,也称为“按对象调用”或“按对象引用调用”。在

这是extensively discussed before。从那篇文章中:

From time to time, people who’ve read a little CS but not a lot CS (or too much of just one kind of CS) pop up on comp.lang.python and waste a lot of energy trying to tell everyone that Python’s using some calling model that it doesn’t really use. It always turns out that they don’t really understand Python’s model, and quite often, they don’t understand their favourite model either.

But nevermind, the only thing you need to know is that Python’s model is neither “call by value” nor “call by reference” (because any attempt to use those terms for Python requires you to use non-standard definitions of the words “-value” and “-reference”). The most accurate description is CLU’s “call by object” or “call by sharing“. Or, if you prefer, “call by object reference“.

You should also read this, if you haven’t done so already.

Python的语义与CLU语言的语义最为相似。Liskov等人的CLU参考手册描述了如下语义:

"We call the argument passing technique call by sharing, because the argument objects are shared between the caller and the called routine. This technique does not correspond to most traditional argument passing techniques (it is similar to argument passing in LISP). In particular it is not call by value because mutations of arguments per- formed by the called routine will be visible to the caller. And it is not call by reference because access is not given to the variables of the caller, but merely to certain objects."

def change1(list1):
        # `list1[1] =` means you are changing the object passed in
        list1[1] = list1[1] + 5

def change2(number):
        # `number = ` means you create a **new local variable**, number, 
        # based on the `number`you passed in
        number = [x+2 for x in number]

因此,如果你想改变现有的对象,你必须以某种方式引用它们,例如

^{pr2}$

更改列表时请注意[ .. ]。在

Python参数是通过引用传递的。您只对change1中的一个对象进行了变异。在

However, numerical values and Strings are all immutable. You cannot change the value of a passed in immutable and see that value change in the caller. Dictionaries and Lists on the other hand are mutable, and changes made to them by a called function will be preserved when the function returns.

更多:http://www.penzilla.net/tutorials/python/functions/

相关问题 更多 >

    热门问题