【编程语言】Python 的参数传递、返回值、浅拷贝、深拷贝

栏目: Python · 发布时间: 4年前

内容简介:作者:LogM本文原载于Python的参数传递,无法控制引用传递还是值传递。对于不可变对象(数字、字符、元组等)的参数,更类似值传递;对于可变对象(列表、字典等),更类似引用传递。

作者:LogM

本文原载于 https://segmentfault.com/u/logm/articles ,不允许转载~

1. Python 的参数传递

Python的参数传递,无法控制引用传递还是值传递。对于不可变对象(数字、字符、元组等)的参数,更类似值传递;对于可变对象(列表、字典等),更类似引用传递。

def fun1(n):
    print(n)   # n在没修改前,指向的地址和main函数中n指向的地址相同
    n = 20     # n在修改后,指向的地址发生改变,相当于新建了一个值为20的参数n

def fun2(l):
    print(l)   # l在没修改前,指向的地址和main函数中l指向的地址相同
    l = [5,6,7,8,9]  # l在修改后,指向的地址和main函数中l指向的地址仍相同,此时main函数中l的值也会改变

if __name__=="__main__":
    n = 10
    l = [1,2,3,4,5]

    fun1(n)
    fun2(l)

    print(n)    # n的值还是10
    print(l)    # l的值在fun2()中已经被改变

2. Python 的返回值

Python的返回值,无法控制引用传递还是值传递。对于不可变对象(数字、字符、元组等)的参数,更类似值传递;对于可变对象(列表、字典等),更类似引用传递。

str1 = 'hi'
l1 = [1,2,3,4,5]

def fun1():
    return str1

def fun2():
    return l1

str2 = fun1()  # 此时,str1 和 str2 的指向地址是相同的
str2 = 'hello'  # 修改后,str2 指向的地址发生改变,相当于新建了一个值为`hello`的变量

l2 = fun2()  # 此时,l1 和 l2 的指向地址是相同的
l2 = [6,7,8,9]  # 修改后,l2 指向的地址仍相同,修改 l2 会影响到 l1

3. Python 的浅拷贝和深拷贝

对于不可变对象(数字、字符、元组等),直接赋值的结果和深拷贝一致;对于可变对象(列表、字典等),直接赋值、浅拷贝、深拷贝结果不同。

# 不可变对象(数字、字符、元组等)的例子

a = 100  
b = a
b = 30

print('a: ', a)
print('b: ', b)

# 结果:
# a = 100
# b = 30
# 可变对象(列表、字典等)的例子

import copy

dict1 = {1:1, 'user':'test', 'num':[1, 2, 3]}

dict2 = dict1  # 直接赋值,两个字典指向地址一致
dict3 = dict1.copy()  # 浅拷贝,只拷贝到原对象
dict4 = copy.deepcopy(dict1)  # 深拷贝,除了拷贝原对象,也会拷贝子对象
 
dict1[1] = 11
dict1['user'] = '123'
dict1['num'].remove(1)
 
print('原字典修改后:',dict1)
print('直接赋值:',dict2)
print('浅拷贝:',dict3)
print('深拷贝:',dict4)

# 结果
# 原字典修改后:{1:11,'user': '123', 'num': [2, 3]}
# 直接赋值:{1:11,'user': '123', 'num': [2, 3]}
# 浅拷贝:{1:1,'user': 'test', 'num': [2, 3]}
# 深拷贝:{1:1,'user': 'test', 'num': [1, 2, 3]}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Iterative Methods for Sparse Linear Systems, Second Edition

Iterative Methods for Sparse Linear Systems, Second Edition

Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00

Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换