python copy模塊

python copy模塊

 

copy模塊用於對象的拷貝操做python

該模塊只提供了兩個主要的方法:函數

copy.copy:淺複製
copy.deepcopy:深複製

 

 

直接賦值,深拷貝和淺拷貝的區別

直接賦值:簡單地拷貝對象的引用,兩個對象的id相同。就是對象的引用(別名),就是給當前內存中的對象增長一個「標籤」而已。經過使用內置函數 id() ,能夠看出指向內存中同一個對象。

淺拷貝(copy):拷貝父對象,不會拷貝對象的內部的子對象。即淺複製只複製對象自己,沒有複製該對象所引用的對象。A shallow copy constructs a new compound object
and then (to the extentpossible) inserts references into it to the objects found in the original.

深拷貝(deepcopy): copy 模塊的 deepcopy 方法,徹底拷貝了父對象及其子對象。即建立一個新的組合對象,同時遞歸地拷貝全部子對象,新的組合對象與原對象沒有任何關聯。雖然實際上會共享不可變的子對象,但不影響它們的相互獨立性。A deep copy constructs a new compound object
and then, recursively, insertscopies into it of the objects found in the original. 淺拷貝和深拷貝的不一樣僅僅是對組合對象來講,所謂的組合對象就是包含了其它對象的對象,如列表,類實例。而對於數字、字符串以及其它「原子」類型,沒有拷貝一說,產生的都是原對象的引用,因此二者就是同樣的結果了。

 

 

 

直接賦值

a={1:{1,2,3}}
b=a
print(a is b)
print(a ==b )

 

運行結果:spa

True
True

 

b = a: 賦值引用,a 和 b 都指向同一個對象。

 

 

 

淺拷貝

import copy
import functools


@functools.total_ordering
class MyClass:

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name

    def __gt__(self, other):
        return self.name > other.name


a = MyClass('a')
my_list = [a]
dup = copy.copy(my_list)

print('             my_list:', my_list)
print('                 dup:', dup)
print('      dup is my_list:', (dup is my_list))
print('      dup == my_list:', (dup == my_list))
print('dup[0] is my_list[0]:', (dup[0] is my_list[0]))
print('dup[0] == my_list[0]:', (dup[0] == my_list[0]))

 

運行結果:code

my_list: [<__main__.MyClass object at 0x101f9c160>]
dup: [<__main__.MyClass object at 0x101f9c160>]
dup is my_list: False
dup == my_list: True
dup[0] is my_list[0]: True
dup[0] == my_list[0]: True

 

b = a.copy(): 淺拷貝, a 和 b 是一個獨立的對象,但他們的子對象仍是指向統一對象(是引用)。

 

 

 

深度拷貝

import copy
import functools


@functools.total_ordering
class MyClass:

    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name

    def __gt__(self, other):
        return self.name > other.name


a = MyClass('a')
my_list = [a]
dup = copy.deepcopy(my_list)

print('             my_list:', my_list)
print('                 dup:', dup)
print('      dup is my_list:', (dup is my_list))
print('      dup == my_list:', (dup == my_list))
print('dup[0] is my_list[0]:', (dup[0] is my_list[0]))
print('dup[0] == my_list[0]:', (dup[0] == my_list[0]))

 

運行結果:orm

my_list: [<__main__.MyClass object at 0x101e9c160>]
dup: [<__main__.MyClass object at 0x1044e1f98>]
dup is my_list: False
dup == my_list: True
dup[0] is my_list[0]: False
dup[0] == my_list[0]: True
Customizing Copy Behavior

 

b = copy.deepcopy(a): 深度拷貝, a 和 b 徹底拷貝了父對象及其子對象,二者是徹底獨立的。

 

 

 

定製複製行爲對象

能夠使用* copy__()和* deepcopy__()特殊方法控制複製的方式。blog

import copy
import functools

@functools.total_ordering
class MyClass:
  def __init__(self, name):
    self.name = name
  def __eq__(self, other):
    return self.name == other.name
  def __gt__(self, other):
    return self.name > other.name
  def __copy__(self):
    print('__copy__()')
    return MyClass(self.name)
  def __deepcopy__(self, memo):
    print('__deepcopy__({})'.format(memo))
    return MyClass(copy.deepcopy(self.name, memo))

a = MyClass('a')
sc = copy.copy(a)
dc = copy.deepcopy(a)
__copy__()
__deepcopy__({})
相關文章
相關標籤/搜索