博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ruby 和 Python 当中的引用、浅拷贝和深拷贝
阅读量:4228 次
发布时间:2019-05-26

本文共 2246 字,大约阅读时间需要 7 分钟。

# Ruby[1] pry(main)> a = 1=> 1[2] pry(main)> b = a=> 1[3] pry(main)> a.object_id=> 3[4] pry(main)> b.object_id=> 3[5] pry(main)> a=2=> 2[6] pry(main)> b=> 1# 数组直接赋值给数组不属于拷贝, 只是内存地址的引用[7] pry(main)> array1 = ['a', 'b', 'c']=> ["a", "b", "c"][8] pry(main)> array2 = array1=> ["a", "b", "c"][9] pry(main)> array1 << 'd'=> ["a", "b", "c", "d"][10] pry(main)> array2=> ["a", "b", "c", "d"]# 浅拷贝[11] pry(main)> array1 = ['a', 'b', 'c']=> ["a", "b", "c"][12] pry(main)> array2 = array1.clone=> ["a", "b", "c"][13] pry(main)> array1 << 'd'=> ["a", "b", "c", "d"][14] pry(main)> array2=> ["a", "b", "c"]# 浅拷贝, 只会拷贝第一层, 第二层的内容不会拷贝[16] pry(main)> array1 = ['a', 'b', 'c', [1, 2, 3]]=> ["a", "b", "c", [1, 2, 3]][17] pry(main)> array2 = array1.clone=> ["a", "b", "c", [1, 2, 3]][18] pry(main)> array1[3] << 4=> [1, 2, 3, 4][19] pry(main)> array2=> ["a", "b", "c", [1, 2, 3, 4]]# 深拷贝,拷贝后的对象操作和原对象没有任何羁绊(关联)。[28] pry(main)> array1 = ['a', 'b', 'c', [1, 2, 3]]=> ["a", "b", "c", [1, 2, 3]][29] pry(main)> array2 = Marshal.load(Marshal.dump(array1))=> ["a", "b", "c", [1, 2, 3]][30] pry(main)> array1[3] << 4=> [1, 2, 3, 4][31] pry(main)> array1=> ["a", "b", "c", [1, 2, 3, 4]][32] pry(main)> array2=> ["a", "b", "c", [1, 2, 3]]

 

# Python3a = 1b = aprint(a, b)print(id(a), id(b))a = 2print(a, b)print(id(a), id(b))"""运行结果1 11445293568 14452935682 11445293568 1445293536"""# 列表直接赋值给列表不属于拷贝, 只是内存地址的引用list1 = ["a", "b", "c"]list2 = list1list1.append("d")print(list1, list2)print(id(list1), id(list2))"""运行结果['a', 'b', 'c', 'd'] ['a', 'b', 'c', 'd']1947385383176 1947385383176"""# 浅拷贝list1 = ["a", "b", "c"]list2 = list1.copy()list1.append("d")print(list1, list2)print(id(list1), id(list2))""" 运行结果:['a', 'b', 'c', 'd'] ['a', 'b', 'c']1553315383560 1553315556936"""# 浅拷贝, 只会拷贝第一层, 第二层的内容不会拷贝list1 = ["a", "b", "c", [1, 2, 3]]list2 = list1.copy()list1[3].append(4)print(list1, list2)print(id(list1), id(list2))"""运行结果['a', 'b', 'c', [1, 2, 3, 4]] ['a', 'b', 'c', [1, 2, 3, 4]]1386655149640 1386655185672"""# 深拷贝,拷贝后的对象操作和原对象没有任何羁绊(关联)。import copy  list1 = ["a", "b", "c", [1, 2, 3]]list2 = copy.deepcopy(list1)list1[3].append(4)print(list1, list2)print(id(list1), id(list2))"""运行结果['a', 'b', 'c', [1, 2, 3, 4]] ['a', 'b', 'c', [1, 2, 3]]1452762592904 1452762606664"""

 

转载地址:http://knjqi.baihongyu.com/

你可能感兴趣的文章
Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications
查看>>
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
查看>>
Moodle E-learning Course Development
查看>>
VoIP For Dummies
查看>>
Administrator's Guide to SQL Server 2005
查看>>
Ajax Design Patterns
查看>>
DNS and BIND (5th Edition)
查看>>
Firewall Fundamentals
查看>>
Learning PHP and MySQL
查看>>
Agile Software Construction
查看>>
Computer Security Basics
查看>>
Sams Teach Yourself MySQL in 10 Minutes
查看>>
Information Systems : The State of the Field
查看>>
IPv6 Essentials
查看>>
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner
查看>>
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
查看>>
Pro .NET 2.0 Windows Forms and Custom Controls in C#
查看>>
Beginning Regular Expressions
查看>>
Beginning Visual Web Developer 2005 Express: From Novice to Professional
查看>>
Beginning Programming
查看>>