Python passing list as argument

前端 未结 4 607
情书的邮戳
情书的邮戳 2020-11-27 07:08

If i were to run this code:

def function(y):
    y.append(\'yes\')
    return y

example = list()
function(example)
print(example)

Why woul

4条回答
  •  自闭症患者
    2020-11-27 07:25

    The easiest way to modify the code will be add the [:] to the function call.

    def function(y):
        y.append('yes')
        return y
    
    
    
    example = list()
    function(example[:])
    print(example)
    

提交回复
热议问题