“Python Salin objek” Kode Jawaban

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

Python Salin objek

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 “Python Salin objek”

Pertanyaan yang mirip dengan “Python Salin objek”

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

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya