The technique of creating a copy of an object is called a copy (also called a clone). We divide the copy into shallow copy and deep copy.Shallow copy: Copies all the fields in the object to the new object (copy). Where the value of the value Type field is copied to the copy, the modifications in the copy do not affect the value corresponding to the source object. While a reference type's field is copied to a reference of a reference type, instead of a
All tools (MAT, yourkit, jprofiler, tptp, etc.) that contain the heap profling function use two terms: shallow size and retained size.These are two terms that are not very common at ordinary times. This article will give a detailed explanation of these two terms.
Shallow sizeThe memory size occupied by the object itself, excluding the objects referenced by the object.For non-array objects, its size is the s
Before understanding shallow and deep copies, first understand how variables are stored in Python.The type of the variable is the two types of the score reference and the address reference.All variables in Python are objects, stored in variables, using address references, storing only the memory address where the value of a variable resides, not the variable itself.In Python, there are multiple data types: bool, int, long, float, string, list, dict, t
, there should be two objects exist, but the actual program running, the output is 1, reflecting only 1 objects. In addition, when the object is destroyed, the class's destructor is called two times because the destroyed two objects are called, and the counter becomes negative. These problems occur most fundamentally in the replication of objects, the counter is not incremented, the solution is to rewrite the copy constructor, in the copy constructor added to the counter processing, the form of
, when calling the Clone method, the allocated memory and the source object (that is, the object called the Clone method) is the same, and then use the corresponding fields in the original object, populate the new object's domain, after the completion of the Clone method returns, A new, identical object is created, and the reference to the new object can be published externally. "A detailed explanation of the Clone method in Java"
Copy of Reference //引用拷贝 private static void co
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 divide the basic type and the reference type? You'll understand later.
Let's take a look at
1. Shallow copy and deep copyShallow copy: Copies only the base type of the object, the object type, and still belongs to the original reference.Deep copy: The base class of the object is not tightly copied, and the objects in the original object are also copied. That is, it is entirely a new object.2. The difference between a shallow copy and a deep copyA shallow
Overview
Copy: Copy an object with the same content as the source object
The following two protocols are required to implement a copy
Nscopying
Nsmutablecopying
Copy returns the type of object
Variable, mutablecopy the object returned by the message
Immutable, the object returned by the copy message
Types of copies
Shallow copy, just copy a pointer to the source ob
Assignment statements in Python do not copy objects, they create bindings between a target and an object. for collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. this module provides generic shallow and deep copy operations (explained below ).
Interface summary:
copy.
copy
(
x
)
return a
Basic JavaScript method of deep copy (shallow copy and deep copy), javascript depth
Preface
When it comes to deep copy, we must first mention the JavaScript data type. In the previous article, the basic JavaScript method-the data type is very clear, so I will not talk about it here.
You need to know that JavaScript data types are classified into basic data types and reference data types.
There is no difference between the two copies for the basic data
In C + +, it is often a matter of shallow copying and deep copying of class objects, which is also an error-prone place.Find relevant information, about shallow copy and deep copy is defined as: Copy the class when the bitwise copy, that is, the value of the data members of an object is copied to the target object. When a pointer type data member is involved in a class, a pointer hanging problem is often ge
There are shallow and deep copies in many development languages, and here's a simple way to differentiate between them in JavaScript and the implementation of deep copies in jquery.Before you talk about shallow and deep copies, you should be aware of the two concepts of access by value and access by reference in JavaScript.Access by value is for the base type (string, number, Boolean, null, undefined). The
; } Cexample (const cexample C) is our custom copy constructor. As can be seen, the copy constructor is a special constructor, the name of the function must be the same as the class name, and its only parameter is a reference variable of this type, which is a const type and immutable. For example, the copy constructor for Class X is in the form of X (x x).When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy constructo
shallow copy, which is explained later.Custom copy constructors are a good programming style that prevents the compiler from forming a default copy constructor, which improves the efficiency of the source code.Shallow copy and deep copyIn some cases, in-class member variables need to dynamically open up heap memory, if a bit copy is implemented, that is, the value of the object is completely copied to another object, such as A=b. At this point, if a
In Python, it is common to copy a list. For replication, there is a natural deep copy and a shallow copy problem. The difference between a deep copy and a shallow copy is that when a new list is copied from the original list, modifying any one of them will affect the other, that is, whether the two lists are stored in the same area in memory, which is an important basis for distinguishing between deep and
The recent project encountered a Python shallow copy mechanism caused by the bug, because of Python object reference, assignment, shallow copy/deep copy mechanism does not have enough knowledge, resulting in debugging a long time to find the problem, here is a simple record of the relevant concepts.In Python's design philosophy, everything in Python is an object, and there is a ob_refcnt variable that maint
When summarizing Python objects and references, it is also possible to understand the deep and shallow copies of Python in fact.In Python, an object's assignment is actually a reference to the object. That is, when you create an object and then assign it to another variable, you are actually copying the reference to the object.We'll start by using the slice operation and the Factory method list method to illustrate the
The main introduction is the Copy module in Python.
The copy module includes deep-shading functions that create composite objects, including lists, tuples, dictionaries, and instances of user-defined objects.
########
Copy (x)
########
Creates a new composite object and creates a shallow copy of x by referencing the members of the copy X. In a more profound sense,
It duplicates the object, but the reference is still used for the elements in the objec
This article is in the "Python core programming 2" see, feel very useful to write out, for everyone reference reference!
Shallow copy
First we use two ways to copy objects, one is slicing, the other is a factory method. Then use the ID function to see their identifiersCopy the Code code as follows:
# Encoding=utf-8
obj = [' name ', [' age ', 18]]
a=obj[:]
B=list (obj)
For x in Obj,a,b:
Print ID (x)
35217032
35227912
29943304
Their IDs are di
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.