Skip to content

Commit b12aa79

Browse files
committed
добавил новые объекты
1 parent 6e02ec0 commit b12aa79

5 files changed

Lines changed: 1675 additions & 1 deletion

Python/FirstPythonTest.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,73 @@
135135
" print(students)\n"
136136
]
137137
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 1,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"x = 100"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 2,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"y = 100"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 3,
159+
"metadata": {},
160+
"outputs": [],
161+
"source": [
162+
"x += 2"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 4,
168+
"metadata": {},
169+
"outputs": [
170+
{
171+
"data": {
172+
"text/plain": [
173+
"102"
174+
]
175+
},
176+
"execution_count": 4,
177+
"metadata": {},
178+
"output_type": "execute_result"
179+
}
180+
],
181+
"source": [
182+
"x"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": 5,
188+
"metadata": {},
189+
"outputs": [
190+
{
191+
"data": {
192+
"text/plain": [
193+
"100"
194+
]
195+
},
196+
"execution_count": 5,
197+
"metadata": {},
198+
"output_type": "execute_result"
199+
}
200+
],
201+
"source": [
202+
"y"
203+
]
204+
},
138205
{
139206
"cell_type": "code",
140207
"execution_count": null,
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Shallow copy - поверхностная копия\n",
10+
"# Deep copy - глубокая копия"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 5,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"[1, 2, 3, [4, 5, 6, 7]]\n",
23+
"[1, 2, 3, [4, 5, 6, 7]]\n"
24+
]
25+
}
26+
],
27+
"source": [
28+
"list1 = [1,2,3 ,[4,5,6]]\n",
29+
"\n",
30+
"copied_list = list1.copy() # shallow copy\n",
31+
"copied_list[3].append(7)\n",
32+
"\n",
33+
"print(list1)\n",
34+
"print(copied_list)"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 6,
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"import copy\n"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 7,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"[1, 2, 3, [4, 5, 6, 7, 8]]\n",
56+
"[1, 2, 3, [4, 5, 6, 7, 8]]\n"
57+
]
58+
}
59+
],
60+
"source": [
61+
"shallow_copy = copy.copy(list1) # Shallow copy - не глубокая копия\n",
62+
"shallow_copy[3].append(8)\n",
63+
"\n",
64+
"print(list1)\n",
65+
"print(shallow_copy)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 8,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"name": "stdout",
75+
"output_type": "stream",
76+
"text": [
77+
"[1, 2, 3, [4, 5, 6, 7, 8]]\n",
78+
"[1, 2, 3, [4, 5, 6, 7, 8, 9]]\n"
79+
]
80+
}
81+
],
82+
"source": [
83+
"deep_copy = copy.deepcopy(list1) # Deep copy - глубокая копия\n",
84+
"deep_copy[3].append(9)\n",
85+
"\n",
86+
"print(list1)\n",
87+
"print(deep_copy)"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 9,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"# Применение копий в объектах"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 10,
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"class Point():\n",
106+
" \n",
107+
" def __init__(self, x, y):\n",
108+
" self.x = x\n",
109+
" self.y = y\n",
110+
" \n",
111+
" def __repr__(self):\n",
112+
" return f'Point({self.x}, {self.y})'"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 11,
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"a = Point(1, 2)\n",
122+
"b = copy.copy(a)"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 12,
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"a.x = 3"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 14,
137+
"metadata": {},
138+
"outputs": [
139+
{
140+
"name": "stdout",
141+
"output_type": "stream",
142+
"text": [
143+
"Point(3, 2)\n",
144+
"Point(1, 2)\n"
145+
]
146+
}
147+
],
148+
"source": [
149+
"print(a) # так как int - числа не ссылочный тип то в b изменений не будет\n",
150+
"print(b)"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 22,
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"class Line():\n",
160+
" \n",
161+
" def __init__(self, p1, p2):\n",
162+
" self.p1 = p1\n",
163+
" self.p2 = p2\n",
164+
" \n",
165+
" def __copy__(self): # функция отвечает за не глубокое копирование\n",
166+
" cls = self.__class__ # палучим данные по текущему типу\n",
167+
" result = cls.__new__(cls) # создаем новый экземпляр через привелигированный (дандр) метод\n",
168+
" result.__dict__.update(self.__dict__) # создаем копию объекта и проапдейтим его атрибуты\n",
169+
" return result\n",
170+
" \n",
171+
" def __deepcopy__(self, memo): \n",
172+
" cls = self.__class__ # палучим данные по текущему типу\n",
173+
" result = cls.__new__(cls) # создаем новый экземпляр через привелигированный (дандр) метод\n",
174+
" memo[id(self)] = result # избегаем бесконечного цикла\n",
175+
" \n",
176+
" for k, v in self.__dict__.items(): # пройдемся по items\n",
177+
" setattr(result, k, copy.deepcopy(v, memo)) # устанавливаем значения в атрибуты \n",
178+
" \n",
179+
" return result"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 23,
185+
"metadata": {},
186+
"outputs": [
187+
{
188+
"name": "stdout",
189+
"output_type": "stream",
190+
"text": [
191+
"Point(6, 2)\n",
192+
"Point(6, 2)\n"
193+
]
194+
}
195+
],
196+
"source": [
197+
"l1 = Line(a, b)\n",
198+
"l2 = copy.copy(l1)\n",
199+
"\n",
200+
"print(l1.p1)\n",
201+
"print(l2.p1)"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 24,
207+
"metadata": {},
208+
"outputs": [
209+
{
210+
"name": "stdout",
211+
"output_type": "stream",
212+
"text": [
213+
"Point(4, 2)\n",
214+
"Point(4, 2)\n"
215+
]
216+
}
217+
],
218+
"source": [
219+
"l1.p1.x = 4\n",
220+
"\n",
221+
"print(l1.p1) # так как Point - ссылочный тип то изменениz будут\n",
222+
"print(l2.p1)"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": 25,
228+
"metadata": {},
229+
"outputs": [
230+
{
231+
"name": "stdout",
232+
"output_type": "stream",
233+
"text": [
234+
"Point(4, 2)\n",
235+
"Point(4, 2)\n",
236+
"Point(6, 2)\n",
237+
"Point(4, 2)\n"
238+
]
239+
}
240+
],
241+
"source": [
242+
"l1 = Line(a, b)\n",
243+
"l2 = copy.deepcopy(l1)\n",
244+
"\n",
245+
"print(l1.p1)\n",
246+
"print(l2.p1)\n",
247+
"\n",
248+
"l1.p1.x = 6\n",
249+
"\n",
250+
"print(l1.p1) # использовали глубокое копирование\n",
251+
"print(l2.p1)"
252+
]
253+
},
254+
{
255+
"cell_type": "code",
256+
"execution_count": null,
257+
"metadata": {},
258+
"outputs": [],
259+
"source": []
260+
}
261+
],
262+
"metadata": {
263+
"kernelspec": {
264+
"display_name": "Python 3",
265+
"language": "python",
266+
"name": "python3"
267+
},
268+
"language_info": {
269+
"codemirror_mode": {
270+
"name": "ipython",
271+
"version": 3
272+
},
273+
"file_extension": ".py",
274+
"mimetype": "text/x-python",
275+
"name": "python",
276+
"nbconvert_exporter": "python",
277+
"pygments_lexer": "ipython3",
278+
"version": "3.7.3"
279+
}
280+
},
281+
"nbformat": 4,
282+
"nbformat_minor": 2
283+
}

0 commit comments

Comments
 (0)