“Objek Salin Python” Kode Jawaban

Objek klon Python

import copy

new_ob = copy.deepcopy(old_ob)
Sasso

Python Deepcopy

>>> import copy
>>> nums = [1, 2, 3]
>>> data = {'a': 10, 'b': nums}
>>> data
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy = copy.copy(data)
>>> data_deep = copy.deepcopy(data)
>>> data_copy
{'a': 10, 'b': [1, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
>>> data_copy['a'] += 2
>>> nums[1:1] = [254]
>>> data
{'a': 10, 'b': [1, 254, 2, 3]}
>>> data_copy
{'a': 12, 'b': [1, 254, 2, 3]}
>>> data_deep
{'a': 10, 'b': [1, 2, 3]}
Attractive Albatross

Objek Salin Python

import copy
class Example:
  def do_something(self): print("Doing a function")
a = Example()
b = copy.deepcopy(a);
b.do_something()
# >>> Doing a function
Edi Sugiarto

Variabel salinan Python

>>> import copy
>>> a = 0
>>> b = 2
>>> a = copy.copy(b)
>>> b += 1
>>> a
2
>>> b
3
Kodi4444

Jawaban yang mirip dengan “Objek Salin Python”

Pertanyaan yang mirip dengan “Objek Salin Python”

Lebih banyak jawaban terkait untuk “Objek Salin Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya