Skip to content

Commit bd27a29

Browse files
committed
Merge branch 'issue/7'
2 parents 3101cf7 + 6cd3b52 commit bd27a29

File tree

1 file changed

+170
-14
lines changed

1 file changed

+170
-14
lines changed

notebooks/PythonObjects.ipynb

Lines changed: 170 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@
778778
},
779779
{
780780
"cell_type": "code",
781-
"execution_count": 86,
781+
"execution_count": null,
782782
"id": "032da8ae",
783783
"metadata": {},
784784
"outputs": [],
@@ -835,7 +835,7 @@
835835
" l_name = property(fget=_get_last_name, \n",
836836
" fset=_set_last_name,\n",
837837
" fdel=_del_last_name,\n",
838-
" doc=\"Last name property.\"\n",
838+
" doc=\"Last Name property.\"\n",
839839
" )"
840840
]
841841
},
@@ -1170,15 +1170,19 @@
11701170
},
11711171
{
11721172
"cell_type": "code",
1173-
"execution_count": 1,
1173+
"execution_count": 7,
11741174
"id": "e4a91bd1",
11751175
"metadata": {},
11761176
"outputs": [],
11771177
"source": [
11781178
"class MyClass:\n",
11791179
" class_var = 10\n",
1180+
"\n",
1181+
" def __init__(self, a=5, b=10):\n",
1182+
" self.a = a\n",
1183+
" self.b = b\n",
11801184
" \n",
1181-
" def method(self, a, b):\n",
1185+
" def set_method(self, a, b):\n",
11821186
" self.a = a\n",
11831187
" self.b = b\n",
11841188
" return 'instance method called', self, self.__add()\n",
@@ -1197,29 +1201,29 @@
11971201
},
11981202
{
11991203
"cell_type": "code",
1200-
"execution_count": 2,
1204+
"execution_count": 9,
12011205
"id": "132ecac5",
12021206
"metadata": {},
12031207
"outputs": [
12041208
{
12051209
"data": {
12061210
"text/plain": [
1207-
"('instance method called', <__main__.MyClass at 0x7fe1e021e710>, 15)"
1211+
"('instance method called', <__main__.MyClass at 0x78cb4862cd40>, 15)"
12081212
]
12091213
},
1210-
"execution_count": 2,
1214+
"execution_count": 9,
12111215
"metadata": {},
12121216
"output_type": "execute_result"
12131217
}
12141218
],
12151219
"source": [
12161220
"instance = MyClass()\n",
1217-
"instance.method(2, 3)"
1221+
"instance.set_method(2, 3)"
12181222
]
12191223
},
12201224
{
12211225
"cell_type": "code",
1222-
"execution_count": 104,
1226+
"execution_count": 12,
12231227
"id": "aeca739a",
12241228
"metadata": {},
12251229
"outputs": [
@@ -1229,7 +1233,7 @@
12291233
"('class method called', __main__.MyClass, 18)"
12301234
]
12311235
},
1232-
"execution_count": 104,
1236+
"execution_count": 12,
12331237
"metadata": {},
12341238
"output_type": "execute_result"
12351239
}
@@ -1241,7 +1245,7 @@
12411245
},
12421246
{
12431247
"cell_type": "code",
1244-
"execution_count": 105,
1248+
"execution_count": 13,
12451249
"id": "961ace48",
12461250
"metadata": {},
12471251
"outputs": [
@@ -1251,7 +1255,7 @@
12511255
"('static method called', 18)"
12521256
]
12531257
},
1254-
"execution_count": 105,
1258+
"execution_count": 13,
12551259
"metadata": {},
12561260
"output_type": "execute_result"
12571261
}
@@ -1261,6 +1265,158 @@
12611265
"MyClass.staticmethod(3, 5)"
12621266
]
12631267
},
1268+
{
1269+
"cell_type": "code",
1270+
"execution_count": 14,
1271+
"id": "1968f5cf",
1272+
"metadata": {},
1273+
"outputs": [],
1274+
"source": [
1275+
"# multiple instances of MyClass is allowed and expected\n",
1276+
"instance1 = MyClass()\n",
1277+
"instance2 = MyClass()"
1278+
]
1279+
},
1280+
{
1281+
"cell_type": "code",
1282+
"execution_count": 15,
1283+
"id": "de470975",
1284+
"metadata": {},
1285+
"outputs": [
1286+
{
1287+
"data": {
1288+
"text/plain": [
1289+
"False"
1290+
]
1291+
},
1292+
"execution_count": 15,
1293+
"metadata": {},
1294+
"output_type": "execute_result"
1295+
}
1296+
],
1297+
"source": [
1298+
"# those instances are in fact different objects\n",
1299+
"instance1 is instance2"
1300+
]
1301+
},
1302+
{
1303+
"cell_type": "markdown",
1304+
"id": "1f17389d",
1305+
"metadata": {},
1306+
"source": [
1307+
"## Singleton Pattern\n",
1308+
"\n",
1309+
"- also called anti-pattern because there is only one instance of the class\n",
1310+
"- use `__new__()` method to create a new instance of the class\n",
1311+
"- use `cls` parameter to check if the instance is already created\n",
1312+
"- return the instance if it's already created\n",
1313+
"- otherwise, create a new instance and return it"
1314+
]
1315+
},
1316+
{
1317+
"cell_type": "code",
1318+
"execution_count": 41,
1319+
"id": "4e8f863a",
1320+
"metadata": {},
1321+
"outputs": [],
1322+
"source": [
1323+
"from __future__ import annotations\n",
1324+
"\n",
1325+
"class MySingletonClass:\n",
1326+
" _instance: \"MyClass\" | None = None\n",
1327+
"\n",
1328+
" # singleton pattern\n",
1329+
" def __new__(cls, *args, **kwargs):\n",
1330+
" # we don't care about args and kwargs within __new__\n",
1331+
" # not providing them will create syntax error because __init__ is defined with two arguments \n",
1332+
" if not cls._instance:\n",
1333+
" cls._instance = super().__new__(cls)\n",
1334+
" return cls._instance\n",
1335+
"\n",
1336+
" def __init__(self, a, b):\n",
1337+
" self.a = a\n",
1338+
" self.b = b\n",
1339+
"\n",
1340+
" def __str__(self):\n",
1341+
" return f'{self.a}, {self.b}'\n",
1342+
" \n",
1343+
" def __eq__(self, other):\n",
1344+
" return self.a == other.a and self.b == other.b"
1345+
]
1346+
},
1347+
{
1348+
"cell_type": "code",
1349+
"execution_count": 42,
1350+
"id": "a764ca44",
1351+
"metadata": {},
1352+
"outputs": [],
1353+
"source": [
1354+
"obj1 = MySingletonClass(2, 3)\n",
1355+
"obj2 = MySingletonClass(3, 4)"
1356+
]
1357+
},
1358+
{
1359+
"cell_type": "code",
1360+
"execution_count": 43,
1361+
"id": "b9b63da3",
1362+
"metadata": {},
1363+
"outputs": [
1364+
{
1365+
"data": {
1366+
"text/plain": [
1367+
"True"
1368+
]
1369+
},
1370+
"execution_count": 43,
1371+
"metadata": {},
1372+
"output_type": "execute_result"
1373+
}
1374+
],
1375+
"source": [
1376+
"obj1 is obj2"
1377+
]
1378+
},
1379+
{
1380+
"cell_type": "code",
1381+
"execution_count": 44,
1382+
"id": "b302bf1f",
1383+
"metadata": {},
1384+
"outputs": [
1385+
{
1386+
"name": "stdout",
1387+
"output_type": "stream",
1388+
"text": [
1389+
"3, 4\n",
1390+
"3, 4\n"
1391+
]
1392+
}
1393+
],
1394+
"source": [
1395+
"print(obj1)\n",
1396+
"print(obj2)"
1397+
]
1398+
},
1399+
{
1400+
"cell_type": "code",
1401+
"execution_count": 45,
1402+
"id": "ad9b3855",
1403+
"metadata": {},
1404+
"outputs": [
1405+
{
1406+
"data": {
1407+
"text/plain": [
1408+
"True"
1409+
]
1410+
},
1411+
"execution_count": 45,
1412+
"metadata": {},
1413+
"output_type": "execute_result"
1414+
}
1415+
],
1416+
"source": [
1417+
"obj1 == obj2"
1418+
]
1419+
},
12641420
{
12651421
"cell_type": "markdown",
12661422
"id": "bb3b269c",
@@ -1330,7 +1486,7 @@
13301486
],
13311487
"metadata": {
13321488
"kernelspec": {
1333-
"display_name": "oop",
1489+
"display_name": "Python 3",
13341490
"language": "python",
13351491
"name": "python3"
13361492
},
@@ -1344,7 +1500,7 @@
13441500
"name": "python",
13451501
"nbconvert_exporter": "python",
13461502
"pygments_lexer": "ipython3",
1347-
"version": "3.10.9"
1503+
"version": "3.12.1"
13481504
}
13491505
},
13501506
"nbformat": 4,

0 commit comments

Comments
 (0)