Skip to content

Commit 65284e6

Browse files
author
bfrerot
committed
17092025
1 parent 844b0c3 commit 65284e6

5 files changed

Lines changed: 332 additions & 124 deletions

File tree

‎MOVING_CASTLE.code-workspace‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"folders": [
3+
{
4+
"name": "PythonLearning",
5+
"path": "."
6+
},
7+
{
8+
"name": "MySQL",
9+
"path": "../MySQL"
10+
},
11+
{
12+
"name": "DataAnalytics",
13+
"path": "../DataAnalytics"
14+
},
15+
{
16+
"name": "DataScience",
17+
"path": "../DataScience"
18+
},
19+
{
20+
"name": "Linux - LPIC",
21+
"path": "../Linux - LPIC"
22+
},
23+
{
24+
"path": "../terraform_gcp"
25+
},
26+
{
27+
"path": "../terraform_aws"
28+
},
29+
{
30+
"path": "../terraform_azure"
31+
}
32+
],
33+
"settings": {
34+
"svg.preview.background": "white"
35+
}
36+
}

‎OOP-CLASS&OBJECT.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,31 @@ def __init__(self):
581581

582582
print(len(stack_object._Stack__stack_list))
583583
# 0
584+
585+
586+
587+
##### DEEP copy
588+
589+
import copy
590+
591+
class Example:
592+
def __init__(self): # __init__() method is executed only once, despite the fact we have two instances of the Example class
593+
self.properties = ["112", "997"]
594+
print("Hello from __init__()")
595+
596+
a_example = Example()
597+
# Hello from __init__()
598+
599+
b_example = copy.deepcopy(a_example)
600+
601+
print("Memory chunks:", id(a_example), id(b_example))
602+
# Memory chunks: 2098880150704 2098883041616
603+
604+
print("Same memory chunk?", a_example is b_example)
605+
# Same memory chunk? False
606+
607+
b_example.properties.append("911")
608+
print('a_example.properties:', a_example.properties)
609+
# a_example.properties: ['112', '997']
610+
print('b_example.properties:', b_example.properties)
611+
# b_example.properties: ['112', '997', '911']

‎PythonLearning.code-workspace‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"folders": [
3+
{
4+
"name": "PythonLearning",
5+
"path": "."
6+
},
7+
{
8+
"name": "MySQL",
9+
"path": "../MySQL"
10+
},
11+
{
12+
"name": "DataAnalytics",
13+
"path": "../DataAnalytics"
14+
},
15+
{
16+
"name": "DataScience",
17+
"path": "../DataScience"
18+
},
19+
{
20+
"name": "Linux - LPIC",
21+
"path": "../Linux - LPIC"
22+
},
23+
{
24+
"path": "../terraform_gcp"
25+
},
26+
{
27+
"path": "../terraform_aws"
28+
},
29+
{
30+
"path": "../terraform_azure"
31+
}
32+
],
33+
"settings": {
34+
"svg.preview.background": "white"
35+
}
36+
}

‎Type-DICTIONARY.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,51 @@
143143
res += data[d]
144144
print(res)
145145
# 6
146+
147+
148+
### copy between dictionaries
149+
150+
# SHALLOW copy
151+
152+
import copy
153+
154+
a_dict = {
155+
'first name': 'James',
156+
'last name': 'Bond',
157+
'movies': ['Goldfinger (1964)', 'You Only Live Twice']
158+
}
159+
b_dict = (a_dict)
160+
print('Memory chunks:', id(a_dict), id(b_dict))
161+
# Memory chunks: 2452251144960 2452251144960
162+
163+
print('Same memory chunk?', a_dict is b_dict)
164+
# Same memory chunk? True
165+
166+
a_dict['movies'].append('Diamonds Are Forever (1971)')
167+
print('a_dict movies:', a_dict['movies'])
168+
# a_dict movies: ['Goldfinger (1964)', 'You Only Live Twice', 'Diamonds Are Forever (1971)']
169+
print('b_dict movies:', b_dict['movies'])
170+
# b_dict movies: ['Goldfinger (1964)', 'You Only Live Twice', 'Diamonds Are Forever (1971)']
171+
172+
# DEEP copy
173+
174+
import copy
175+
176+
a_dict = {
177+
'first name': 'James',
178+
'last name': 'Bond',
179+
'movies': ['Goldfinger (1964)', 'You Only Live Twice']
180+
}
181+
b_dict = copy.deepcopy(a_dict)
182+
print('Memory chunks:', id(a_dict), id(b_dict))
183+
# Memory chunks: 2349147026176 2349144587520
184+
185+
print('Same memory chunk?', a_dict is b_dict)
186+
# Same memory chunk? False
187+
188+
a_dict['movies'].append('Diamonds Are Forever (1971)')
189+
print('a_dict movies:', a_dict['movies'])
190+
# a_dict movies: ['Goldfinger (1964)', 'You Only Live Twice', 'Diamonds Are Forever (1971)']
191+
print('b_dict movies:', b_dict['movies'])
192+
# b_dict movies: ['Goldfinger (1964)', 'You Only Live Twice']
193+

0 commit comments

Comments
 (0)