Python deep copy and python copy
Python data structures are divided into two types:
1. String and number
2. List, tuples, and dictionariesI. Strings and numbers
For strings and numbers, assigning values (=), copying, and deepcopy is meaningless because they always point to the same memory address.
>>> Import copy >>> a
What is "clone "?
In the actual programming process, we often encounter this situation: there is an object A, which contains some valid values at a time point, in this case, you may need a new object B that is exactly the same as a, and any subsequent changes to B will not affect the value in A. That is to say, A and B are two independent objects, however, the initial value of B is determined by object. In Java/javasript, a simple value assignment st
Direct Assignment: is actually the object's reference (alias).
shallow copy (copy): copies the parent object and does not copy the inner sub-object of the object.
deep Copy (deepcopy): The Deepcopy method of the Copy modul
By releasing the comments one by one in the IDE in the following different situations, you understandImport Copy "" "First case, immutable type variable, all references" "" "# a = # a = (one, 222, 333) # a = ' string ' # a = true# B = a## print (ID (a)) # Print (ID (b)) # # C =copy.copy (a) # d = Copy.deepcopy (a) # Print (ID (c)) # Print (ID (d)) "" "" "second case, variable type variable, shallow copy and
Introduction to python replication (deep copy, shallow copy) and python
Blog reference: click here
In python, there are differences between object replication and shallow copy and deep copy. Here we mainly use variable variables f
the contents of the variable pointing to the two address will change but the address points to the sameIf the value is re-assigned, the re-assigned variable address will point to the new memory addressThis is the process of assigning a variable value.Shallow copyShallow copy: No matter how complex the data structure, shallow copy will only copy one layer.A
deep copy : When an object contains a pointer field, it is initialized between objects (that is, when the copy constructor is called) or = operation (note: Shallow both are different cases), and the contents of the memory space contained in the pointer are also copiedShallow copy: When an object contains a pointer fiel
In Python, there is a difference between the assignment of an object and the copy (deep/shallow copy), which can produce unexpected results if used without notice.First of all, we should have the following knowledge about the assignment operation:
Assignment is to assign the address of an object to a variable, so that the variable points to that address
This article is to share with you about how to understand the object reference in PHP, shallow copy and deep copy, the need for friends can refer to
Many novice phper to OOP programming is easy to ignore these concepts, the recent reading of the junior PHP engineer this part of the object reference is more vague, looked up some information, understand, want to d
Project, you will often encounter a copy of the object you want to create as a temporary variable or other use, you need a new object to come out,It then assigns the various properties of the source object to the new object, so that the properties of the new object are changed in time, and the source object is not changed, that is, the deep copy.Obviously, it is not only tedious and error-prone to assign a
In C + + deep copy and shallow copy problem is more important, a brief introduction to the meaning of deep copy and shallow copy, in C + + class by default There are six functions, copy
can see, after modifying the value of reference type A in B1, the value of a in B2 also changes. Further, it can be judged that B1 A and B2 A are the same object.2. Deep copyLike C + +, PHP also provides copy constructors, which allow you to customize replication behavior for deep copies. PHP accomplishes the copy con
One, shallow copy and deep copyThe so-called shallow copy, which is implemented by the default copy constructor, assigns values to the data members individually. If the class contains pointer-type data, this method simply assigns pointers to new members, but does not allocate memory to new members, so this approach ine
light copy and deep copy
Shallow copy: assign values to members.
Stringbad sailor = sports; equivalent
Stringbad sailor;
Sailor. Len = sports. Len;
Sailor. Str = sports. STR;
For a shortest copy, two pointers point to the STR string. When the Destructor is called, STR memo
C # is a copy of a light copy and a deep copy.For example, object A, which has a value type field and a reference type field:1. Shallow copy:For a value Type field, copying directly to a copy of the new Copy object, modifying the value of the copy's field does not affect the
Before we talk about the shallow copy and deep copy of JavaScript, we need to discuss the data type of JS. We all know there are five types of number,boolean,string,null,undefined,object. And object also contains Function,array and object itself. The previous five types are called basic types, while object is a reference type. One might ask, why do you want to di
different objects, using the built-in function ID () to discover that the identities of the two are different.In fact, Python has a special case, for example:A = 1b = 1id (a) = 14332248id (b) = 14332248The reason is that Python thinks that these small integers are often used, so python caches a subset of the small integer types. 2.2 Deep copy Shallow copyThe sequence type can be implemented in three wa
1. First of all, for the object to be manipulated, for non-mutable objects, such as string, number, tuple, and so on, these three operations are equivalent, are referenceImport CopyA= ' Apple 'B=aC=copy.copy (a)D=copy.deepcopy (a)Print (ID (a))Print (ID (b))Print (ID (c))Print (ID (d)Output:1840734496184073449618407344961840734496It can be seen that all four variables point to the same memory address, the address where the string "Apple" is located2. For mutable objects (or container objects), s
In this paper, deep asp.net and shallow copy are analyzed. Share to everyone for your reference. The specific analysis is as follows:
I have not been clear about the difference between depth replication and shallow copy exactly where, today thoroughly understand this thing, write to and home for mutual encouragement.
If you do not understand the difference betw
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.