Python copy object
May 21, 2017 am 10:58 AMWhen assigning values between objects in Python, they are passed by reference. If you need to copy an object, you need to use the copy module in the standard library.
1. copy.copy shallow copy only copies the parent object, and does not copy the internal child objects of the object.
2. copy.deepcopy deep copy copies the object and its sub-objects
A good example:
import copy
a = [1, 2, 3, 4, ['a', 'b']] # Original object
b = a #Assignment, passing the reference of the object
c = copy.copy(a) #Object copy, shallow copy
d = copy. deepcopy(a) #Object copy, deep copy
##a.append( 5) #Modify object a
##a[4].append('c') #Modify the ['a', 'b'] array object in object a
print 'a = ', a
print 'b = ', b
print 'c = ', c
print 'd = ', d
Output result:
a = [1, 2, 3, 4, ['a', 'b', 'c'], 5]
b = [1, 2, 3, 4, ['a', 'b', 'c'], 5]
c = [1, 2, 3, 4, ['a', 'b', 'c']]
d = [ 1, 2, 3, 4, ['a', 'b']]
The above is the detailed content of Python copy object. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis
