Skip to content

Commit b4e6f53

Browse files
committed
Add point example
1 parent 3158989 commit b4e6f53

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

source-code/object-orientation/attr_intro.ipynb

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,223 @@
13871387
"source": [
13881388
"Of course, you can still impliment your own hash method and comparison methods if required."
13891389
]
1390+
},
1391+
{
1392+
"cell_type": "markdown",
1393+
"id": "536b220c-b157-47de-89e6-f4a4946e480c",
1394+
"metadata": {},
1395+
"source": [
1396+
"# Example"
1397+
]
1398+
},
1399+
{
1400+
"cell_type": "markdown",
1401+
"id": "6e2c5118-2211-4487-9055-592b89b5b559",
1402+
"metadata": {},
1403+
"source": [
1404+
"We will implement the `Point` class that was sued as a running example."
1405+
]
1406+
},
1407+
{
1408+
"cell_type": "code",
1409+
"execution_count": 62,
1410+
"id": "391732ee-8952-49e6-857c-e8b8c6e8dab4",
1411+
"metadata": {},
1412+
"outputs": [],
1413+
"source": [
1414+
"from math import sqrt"
1415+
]
1416+
},
1417+
{
1418+
"cell_type": "code",
1419+
"execution_count": 80,
1420+
"id": "2faf536d-83eb-4587-8116-4d665e1cf546",
1421+
"metadata": {},
1422+
"outputs": [],
1423+
"source": [
1424+
"@attr.s\n",
1425+
"class Point:\n",
1426+
" x: float = attr.ib(converter=float)\n",
1427+
" y: float = attr.ib(converter=float)\n",
1428+
" \n",
1429+
" def distance(self, other):\n",
1430+
" return sqrt((self.x - other.x)**2 + (self.y - other.y)**2)\n",
1431+
" \n",
1432+
" @property\n",
1433+
" def coords(self):\n",
1434+
" return (self.x, self.y)\n",
1435+
" \n",
1436+
" @coords.setter\n",
1437+
" def coords(self, value):\n",
1438+
" self.x, self.y = value"
1439+
]
1440+
},
1441+
{
1442+
"cell_type": "code",
1443+
"execution_count": 68,
1444+
"id": "62516ade-9c73-4cb1-8143-110c5141c672",
1445+
"metadata": {},
1446+
"outputs": [
1447+
{
1448+
"data": {
1449+
"text/plain": [
1450+
"(Point(x=3.1, y=5.0), Point(x=3.2, y=1.9))"
1451+
]
1452+
},
1453+
"execution_count": 68,
1454+
"metadata": {},
1455+
"output_type": "execute_result"
1456+
}
1457+
],
1458+
"source": [
1459+
"p1, p2 = Point(3.1, 5), Point('3.2', 1.9)\n",
1460+
"p1, p2"
1461+
]
1462+
},
1463+
{
1464+
"cell_type": "markdown",
1465+
"id": "b1d56498-9adc-4ac3-bdde-1b331414c587",
1466+
"metadata": {},
1467+
"source": [
1468+
"Note the differences with the original \"pure Python\" implementation:\n",
1469+
"* `x` and `y` are public attributes, and\n",
1470+
"* values assigned to the attributes are not validated."
1471+
]
1472+
},
1473+
{
1474+
"cell_type": "code",
1475+
"execution_count": 71,
1476+
"id": "a606d9c1-e392-462b-8420-e88f4735896a",
1477+
"metadata": {},
1478+
"outputs": [
1479+
{
1480+
"data": {
1481+
"text/plain": [
1482+
"Point(x='abc', y=5.0)"
1483+
]
1484+
},
1485+
"execution_count": 71,
1486+
"metadata": {},
1487+
"output_type": "execute_result"
1488+
}
1489+
],
1490+
"source": [
1491+
"p1.x = 'abc' \n",
1492+
"p1"
1493+
]
1494+
},
1495+
{
1496+
"cell_type": "markdown",
1497+
"id": "c4b80743-aad7-4ae2-98d8-c422440ea9cf",
1498+
"metadata": {},
1499+
"source": [
1500+
"If you want that level of control, the advantages of using `attrs` start to decrease considerably."
1501+
]
1502+
},
1503+
{
1504+
"cell_type": "code",
1505+
"execution_count": 74,
1506+
"id": "2daae99c-7ebb-4dab-95a5-edc5984bcdbc",
1507+
"metadata": {},
1508+
"outputs": [
1509+
{
1510+
"data": {
1511+
"text/plain": [
1512+
"Point(x=17.1, y=12.5)"
1513+
]
1514+
},
1515+
"execution_count": 74,
1516+
"metadata": {},
1517+
"output_type": "execute_result"
1518+
}
1519+
],
1520+
"source": [
1521+
"p1.coords = 17.1, 12.5\n",
1522+
"p1"
1523+
]
1524+
},
1525+
{
1526+
"cell_type": "markdown",
1527+
"id": "c4ce5a9c-568c-4c86-be22-9b8f08ec5bda",
1528+
"metadata": {},
1529+
"source": [
1530+
"## Inheritance"
1531+
]
1532+
},
1533+
{
1534+
"cell_type": "markdown",
1535+
"id": "ac2f90f5-3d5e-4dd4-9fe2-4fa56f669ced",
1536+
"metadata": {},
1537+
"source": [
1538+
"Inheritance simply works as expected. Methods and attributes can be added to subclasses."
1539+
]
1540+
},
1541+
{
1542+
"cell_type": "code",
1543+
"execution_count": 75,
1544+
"id": "1a6a3c6d-d177-4f4f-b339-9ba3be1ae264",
1545+
"metadata": {},
1546+
"outputs": [],
1547+
"source": [
1548+
"@attr.s\n",
1549+
"class PointMass(Point):\n",
1550+
" mass: float = attr.ib(converter=float)\n",
1551+
" \n",
1552+
" def __attr_pre_init__(self)"
1553+
]
1554+
},
1555+
{
1556+
"cell_type": "code",
1557+
"execution_count": 78,
1558+
"id": "52e32576-1c2b-4195-868f-4505da5e141e",
1559+
"metadata": {},
1560+
"outputs": [
1561+
{
1562+
"data": {
1563+
"text/plain": [
1564+
"PointMass(x=1.2, y=2.3, mass=5.0)"
1565+
]
1566+
},
1567+
"execution_count": 78,
1568+
"metadata": {},
1569+
"output_type": "execute_result"
1570+
}
1571+
],
1572+
"source": [
1573+
"p1 = PointMass(1.2, 2.3, 5.0)\n",
1574+
"p1"
1575+
]
1576+
},
1577+
{
1578+
"cell_type": "code",
1579+
"execution_count": 82,
1580+
"id": "f40178b8-338d-4afd-95e2-f3b8a6afcc2e",
1581+
"metadata": {},
1582+
"outputs": [],
1583+
"source": [
1584+
"p2 = Point(3.2, 4.1)"
1585+
]
1586+
},
1587+
{
1588+
"cell_type": "code",
1589+
"execution_count": 83,
1590+
"id": "bd49b807-7255-408b-9989-1212f6e8c1df",
1591+
"metadata": {},
1592+
"outputs": [
1593+
{
1594+
"data": {
1595+
"text/plain": [
1596+
"2.690724809414742"
1597+
]
1598+
},
1599+
"execution_count": 83,
1600+
"metadata": {},
1601+
"output_type": "execute_result"
1602+
}
1603+
],
1604+
"source": [
1605+
"p1.distance(p2)"
1606+
]
13901607
}
13911608
],
13921609
"metadata": {

0 commit comments

Comments
 (0)