134 lines
2.6 KiB
Python
Executable File
134 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python
|
|
"""
|
|
# Copia de elementos
|
|
|
|
- Copia de elementos mutables con el modulo 'copy'
|
|
- Diferencias entre copia superficial y copia profunda (shallow & deep copying)
|
|
- Copia exacta de objetos personalizados (custom objects).
|
|
|
|
"""
|
|
|
|
# import copy
|
|
|
|
orig = 5
|
|
|
|
# Nueva variable con la misma referencia
|
|
copia = orig
|
|
|
|
# Nueva variable con otra referencia
|
|
copia = 6
|
|
|
|
print(orig,copia)
|
|
# 5 6
|
|
|
|
# Lista, la copia asigna la misma referencia a nueva variable
|
|
orig = [0, 1, 2, 3]
|
|
copia = orig
|
|
|
|
copia[0] = -10
|
|
|
|
print(orig, copia) # [-10, 1, 2, 3] [-10, 1, 2, 3]
|
|
|
|
|
|
# copia nueva variable, nueva referencia
|
|
copia2 = orig.copy()
|
|
|
|
orig[0] = 9
|
|
|
|
print(orig, copia2) # [9, 1, 2, 3] [-10, 1, 2, 3]
|
|
|
|
|
|
|
|
import copy
|
|
|
|
# shallow copy: Un nivel de profundidad, solo referencias de
|
|
# objetos hijos anidados.
|
|
# make a shallow copy of y
|
|
# x = copy.copy(orig)
|
|
|
|
orig = [0, 1, 2, 3, 4]
|
|
|
|
copia = orig
|
|
copia = copy.copy(orig)
|
|
copia[0] = -10
|
|
|
|
print(copia) # [-10, 1, 2, 3, 4]
|
|
print(orig) # [0, 1, 2, 3, 4]
|
|
|
|
# otras formas de copiar listas superficialmente
|
|
copia = orig.copy()
|
|
copia = list(orig)
|
|
copia = orig[:]
|
|
|
|
# un solo nivel de profundidad
|
|
orig = [[0,1,2,3],[4,5,6,7]]
|
|
copia = orig.copy()
|
|
copia[0][1] = -10
|
|
|
|
print(copia) # [[0, -10, 2, 3], [4, 5, 6, 7]]
|
|
print(orig) # [[0, -10, 2, 3], [4, 5, 6, 7]]
|
|
|
|
# deep copy: Copia completamente independiente
|
|
# make a deep copy of y
|
|
# x = copy.deepcopy(orig)
|
|
orig = [[0,1,2,3],[4,5,6,7]]
|
|
copia = copy.deepcopy(orig)
|
|
copia[0][1] = -10
|
|
|
|
print(copia) # [[0, -10, 2, 3], [4, 5, 6, 7]]
|
|
print(orig) # [[0, 1, 2, 3], [4, 5, 6, 7]]
|
|
|
|
|
|
# copiando clases
|
|
|
|
class Persona:
|
|
def __init__(self, nombre, edad):
|
|
self.nombre = nombre
|
|
self.edad = edad
|
|
|
|
|
|
p1 = Persona('Zafiro', 82)
|
|
p2 = p1
|
|
|
|
p2.edad = 28
|
|
print(p2.edad) # 28
|
|
print(p1.edad) # 28
|
|
|
|
|
|
p2 = copy.copy(p1)
|
|
p2.edad = 46
|
|
print(p2.edad) # 46
|
|
print(p1.edad) # 28
|
|
|
|
|
|
class Compania:
|
|
def __init__(self, jefe, empleado):
|
|
self.jefe = jefe
|
|
self.empleado = empleado
|
|
|
|
|
|
p1 = Persona('Norberto', 61)
|
|
p2 = Persona('Zerafin', 16)
|
|
|
|
compania = Compania(p1, p2)
|
|
compania_copia = copy.copy(compania)
|
|
compania_copia.jefe.edad = 36
|
|
|
|
# copia superficial, solo un nivel mas de profundidad
|
|
# apunta a la misma referencia en este nivel.
|
|
print(compania_copia.jefe.edad) # 36
|
|
print(compania.jefe.edad) # 36
|
|
|
|
|
|
# copia profunda, independiente.
|
|
compania = Compania(p1, p2)
|
|
compania_copia = copy.deepcopy(compania)
|
|
compania_copia.jefe.edad = 64
|
|
|
|
print(compania_copia.jefe.edad) # 64
|
|
print(compania.jefe.edad) # 36
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|