Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, is a function return a shallow or deep copy?

In python, if I have

x = y

any modification to x will also modify y, and I can do

x = deepcopy(y)

if I want to avoid modifying y while working on x

Say, instead, that I have:

myFunc():
    return y

def main():
    x = myFunc()

Is it still the case that modifying x will modify y, or since it is a return from another function it will be like a deepcopy?

like image 778
user Avatar asked Feb 03 '17 20:02

user


1 Answers

In python everything is a reference. Nothing gets copied unless you explicitly copy it.

In your example, x and y reference the same object.

like image 176
shx2 Avatar answered Oct 05 '22 22:10

shx2