简体   繁体   English

在python(jupyter)中复制变量/对相同的变量使用不同的函数

[英]Copy a variable in python (jupyter) / Use different functions with same variables

I wrote a small Programm in python but it don't work like expected. 我用python编写了一个小型Programm,但是它没有按预期工作。

Here's the code: 这是代码:

puzzle = [8, 7, 5, 4, 1, 2, 3, 0, 6]
def count(p):
        p[0] += 1
        return p
def main(p):
    print(p)
    l = count(p)
    print(l)
    print(p)

b1 = main(puzzle)

I expect that print(p) will be different from print(l), but the result of both is the same, it's the result that print(l) should have. 我希望print(p)与print(l)不同,但是两者的结果相同,这是print(l)应该具有的结果。 But p did change also, however I would need it to be unchanged… Is this a special python behavior? 但是p也做了更改,但是我需要将其保持不变……这是一种特殊的python行为吗? Is there something I missed? 有什么我想念的吗? I also tried to change the variable names in the functions, but that didn't help. 我也尝试更改函数中的变量名,但这没有帮助。 I restarted the Compiler, but that didn't help either. 我重新启动了编译器,但这也无济于事。

Is there a solution to store a function output and than call the function again without let the function change the given parameters? 有没有解决方案来存储函数输出,然后再次调用函数而不让函数更改给定参数? So that l will be the result after the calculation and p will stay the value before? 这样,l将成为计算后的结果,而p将保持在该值之前?

Kind Regards, Joh. 亲切的问候,Joh。

You are passing a List parameter. 您正在传递一个List参数。 Parameter passing is Call-by-Object. 参数传递是按对象调用。 Since a List is a mutable object in this situation it is similar to pass by reference and changes to your List object will persist. 由于列表在这种情况下是可变对象,因此类似于通过引用传递,并且对列表对象的更改将继续存在。 If you were passing an immutable, such as an Integer or String, it would be akin to pass by copy/value, and changes would not persist. 如果您传递的是不可变值(例如Integer或String),则类似于通过复制/值传递,并且更改不会持久。 Eg: 例如:

def s2asdf(s):
    s = "asdf"

s = "hello world"
s2asdf(s)
print s

... results in: ... 结果是:

$ python example.py
hello world

The reason for this is because Python passes function parameters by reference. 这样做的原因是因为Python通过引用传递了函数参数。 When you call the count function it allows the function to modify the list inside the function and the changes will be applied to the original object. 当您调用count函数时,它允许该函数修改函数内部的列表,并且更改将应用​​于原始对象。

If you want to have the function not modify the list but instead return a different list, you will have to make a copy of the list either by passing a copy to the function or make a copy inside the function itself. 如果要让函数不修改列表而是返回其他列表,则必须通过将副本传递给函数或在函数内部进行复制来复制列表。 There are many ways to copy a list in Python, but I like to use the list() function to do it. 有很多方法可以在Python中复制列表,但是我喜欢使用list()函数来实现。

This should fix your problem: 这应该可以解决您的问题:

puzzle = [8, 7, 5, 4, 1, 2, 3, 0, 6]
def count(p):
    new_list = list(p)  # copy values of p to new_list
    new_list[0] += 1
    return new_list

def main(p):
    print(p)
    l = count(p)
    print(l)  # l is the new_list returned from count
    print(p)  # p stays the original value

b1 = main(puzzle)

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

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