<jupyter_start><jupyter_text>Deep Copy and Shallow Copy<jupyter_code>class Smurf:
    def __init__(self):
        pass
    
    @property
    def beardcolor(self):
        return self._beardcolor
    
    @beardcolor.setter
    def beardcolor(self, color):
        self._beardcolor = color
        
    def __str__(self):
        
        return f"I'm smurf with {self._beardcolor} beard\n"
smurf = Smurf()
smurf.beardcolor = "blue"
smurf.beardcolor<jupyter_output><empty_output><jupyter_text>Copy using = operator<jupyter_code>same_smurf = smurf

same_smurf.beardcolor

same_smurf.beardcolor = 'orange'

smurf.beardcolor

# Both smurf have the same id, they are the same 
print(smurf)
print(same_smurf)<jupyter_output>I'm smurf with orange beard

I'm smurf with orange beard<jupyter_text>have the original values unchanged and only modify the new values or vice versa- Shallow copy (Affect nested object)- Deep copy (Independent new nested object)  Shallow Copy<jupyter_code>import copy

second_smurf = copy.copy(smurf)

second_smurf.beardcolor

second_smurf.beardcolor = 'pink'

smurf.beardcolor

## Adding nested object in child_list does not impact parent list
parent_list = [smurf, second_smurf]
child_list = copy.copy(parent_list)

third_smurf = Smurf()
third_smurf.beardcolor = "green"
child_list.append(third_smurf)

[print(i) for i in parent_list]
print("-------------------------")
[print(i) for i in child_list]<jupyter_output>I'm smurf with orange beard

I'm smurf with pink beard

-------------------------
I'm smurf with orange beard

I'm smurf with pink beard

I'm smurf with green beard<jupyter_text>See how the address of the nested object is the same<jupyter_code>print([i for i in parent_list])
print("-------------------------")
print([i for i in child_list])<jupyter_output>[<__main__.Smurf object at 0x0000019D76DBC100>, <__main__.Smurf object at 0x0000019D76E6D8E0>]
-------------------------
[<__main__.Smurf object at 0x0000019D76DBC100>, <__main__.Smurf object at 0x0000019D76E6D8E0>, <__main__.Smurf object at 0x0000019D76EAC3A0>]<jupyter_text>Any changes to the nested object will impact both parent and child<jupyter_code>parent_list[0].beardcolor = "unicorn"

[print(i) for i in parent_list]
print("-------------------------")
[print(i) for i in child_list]<jupyter_output>I'm smurf with unicorn beard

I'm smurf with pink beard

-------------------------
I'm smurf with unicorn beard

I'm smurf with pink beard<jupyter_text>Deep Copy<jupyter_code>parent_list = [smurf, second_smurf]
child_list = copy.deepcopy(parent_list)

[print(i) for i in parent_list]
print("-------------------------")
[print(i) for i in child_list]<jupyter_output>I'm smurf with unicorn beard

I'm smurf with pink beard

-------------------------
I'm smurf with unicorn beard

I'm smurf with pink beard<jupyter_text>Different Addresses<jupyter_code>print([i for i in parent_list])
print("-------------------------")
print([i for i in child_list])
parent_list[0].beardcolor = "dark dull black"

[print(i) for i in parent_list]
print("-------------------------")
[print(i) for i in child_list]
child_list[0].beardcolor = "neon_green"

[print(i) for i in parent_list]
print("-------------------------")
[print(i) for i in child_list]<jupyter_output>I'm smurf with dark dull black beard

I'm smurf with pink beard

-------------------------
I'm smurf with neon_green beard

I'm smurf with pink beard<jupyter_text>Shallow copy / deep copy doesnt impact single object (cause it's not nested object)<jupyter_code>test_smurf = copy.copy(smurf)
smurf
test_smurf<jupyter_output><empty_output><jupyter_text>Naturally, the changes to the parent object wont impact the child object<jupyter_code>smurf.beardcolor = "white"
print(smurf)
print(test_smurf)<jupyter_output>I'm smurf with white beard

I'm smurf with dark dull black beard
{'fraction_non_alphanumeric': 0.16086842793617578, 'fraction_numerical': 0.017525503531258174, 'mean_word_length': 3.5578069129916567, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '37629', 'license': 'MIT', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1335, 'n_words': 365, 'path': 'notebooks/class/deepcopy_shallowcopy.ipynb', 'repo_name': 'codenamewei/pydata-science-playground', 'size': '3823'}