|
23 | 23 | "#### 1) [Args and Kwargs](#ch1)\n", |
24 | 24 | "#### 2) [Generators](#ch2)\n", |
25 | 25 | "#### 3) [Map, Reduce, Filter](#ch3)\n", |
26 | | - "#### 4) [Sets](#ch4)" |
| 26 | + "#### 4) [Sets](#ch4)\n", |
| 27 | + "#### 5) [Decorators](#ch5)" |
27 | 28 | ] |
28 | 29 | }, |
29 | 30 | { |
|
48 | 49 | "https://stackoverflow.com/questions/400739/what-does-asterisk-mean-in-python <br>\n", |
49 | 50 | "https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters\n", |
50 | 51 | "\n", |
51 | | - "...\n", |
| 52 | + "Check the above links.\n", |
52 | 53 | "\n", |
53 | 54 | "#### So, what is `*args` doing?\n", |
54 | 55 | "`*args` allows you to pass a desired number of arguments to the function. Let's see an example." |
|
1187 | 1188 | "cell_type": "markdown", |
1188 | 1189 | "metadata": {}, |
1189 | 1190 | "source": [ |
1190 | | - "Cause that's it. The filtered list only contains `[-5, -4, -3, -2, -1]`" |
| 1191 | + "Because that's it. The filtered list only contains `[-5, -4, -3, -2, -1]`" |
1191 | 1192 | ] |
1192 | 1193 | }, |
1193 | 1194 | { |
|
1420 | 1421 | "cell_type": "markdown", |
1421 | 1422 | "metadata": {}, |
1422 | 1423 | "source": [ |
1423 | | - "Remove duplicates from the list." |
| 1424 | + "See the duplicates from the list." |
1424 | 1425 | ] |
1425 | 1426 | }, |
1426 | 1427 | { |
1427 | 1428 | "cell_type": "code", |
1428 | | - "execution_count": null, |
| 1429 | + "execution_count": 96, |
1429 | 1430 | "metadata": {}, |
1430 | | - "outputs": [], |
| 1431 | + "outputs": [ |
| 1432 | + { |
| 1433 | + "data": { |
| 1434 | + "text/plain": [ |
| 1435 | + "{2, 6, 9}" |
| 1436 | + ] |
| 1437 | + }, |
| 1438 | + "execution_count": 96, |
| 1439 | + "metadata": {}, |
| 1440 | + "output_type": "execute_result" |
| 1441 | + } |
| 1442 | + ], |
1431 | 1443 | "source": [ |
1432 | 1444 | "duplicated_list = [1,2,2,4,6,3,9,6,9,7]\n", |
1433 | | - "final_list = set([x for x in duplicated_list if duplicated_list.co])" |
| 1445 | + "final_list = set([x for x in duplicated_list if duplicated_list.count(x)>1])\n", |
| 1446 | + "final_list" |
| 1447 | + ] |
| 1448 | + }, |
| 1449 | + { |
| 1450 | + "cell_type": "markdown", |
| 1451 | + "metadata": {}, |
| 1452 | + "source": [ |
| 1453 | + "<a id=\"ch5\"></a>\n", |
| 1454 | + "## Chapter 5 - Decorators" |
| 1455 | + ] |
| 1456 | + }, |
| 1457 | + { |
| 1458 | + "cell_type": "markdown", |
| 1459 | + "metadata": {}, |
| 1460 | + "source": [ |
| 1461 | + "Decorators are functions which modify the functionality of another function." |
| 1462 | + ] |
| 1463 | + }, |
| 1464 | + { |
| 1465 | + "cell_type": "code", |
| 1466 | + "execution_count": 79, |
| 1467 | + "metadata": {}, |
| 1468 | + "outputs": [ |
| 1469 | + { |
| 1470 | + "data": { |
| 1471 | + "text/plain": [ |
| 1472 | + "20" |
| 1473 | + ] |
| 1474 | + }, |
| 1475 | + "execution_count": 79, |
| 1476 | + "metadata": {}, |
| 1477 | + "output_type": "execute_result" |
| 1478 | + } |
| 1479 | + ], |
| 1480 | + "source": [ |
| 1481 | + "def sum(x,y):\n", |
| 1482 | + " return x+y\n", |
| 1483 | + "\n", |
| 1484 | + "def double(anything):\n", |
| 1485 | + " final = anything*2\n", |
| 1486 | + " return final\n", |
| 1487 | + "\n", |
| 1488 | + "double(sum(4,6)) # Take a sum of 4 and 6 and double that result." |
1434 | 1489 | ] |
| 1490 | + }, |
| 1491 | + { |
| 1492 | + "cell_type": "markdown", |
| 1493 | + "metadata": {}, |
| 1494 | + "source": [ |
| 1495 | + "In this above example, we first defined a `sum()` function that sums up two numbers.<br>\n", |
| 1496 | + "After that, we defined another function named `double()` that takes **function as an argument.**\n", |
| 1497 | + "In our case, the `double` function took the earlier result of the `sum` function and simply doubled it. \n", |
| 1498 | + "Let's try another simple example." |
| 1499 | + ] |
| 1500 | + }, |
| 1501 | + { |
| 1502 | + "cell_type": "code", |
| 1503 | + "execution_count": 80, |
| 1504 | + "metadata": {}, |
| 1505 | + "outputs": [], |
| 1506 | + "source": [ |
| 1507 | + "def multiply(x,y):\n", |
| 1508 | + " return x*y\n", |
| 1509 | + "def final():\n", |
| 1510 | + " print(\"This is a multiplication operation\")\n", |
| 1511 | + " print(multiply(4,4))" |
| 1512 | + ] |
| 1513 | + }, |
| 1514 | + { |
| 1515 | + "cell_type": "code", |
| 1516 | + "execution_count": 81, |
| 1517 | + "metadata": {}, |
| 1518 | + "outputs": [ |
| 1519 | + { |
| 1520 | + "name": "stdout", |
| 1521 | + "output_type": "stream", |
| 1522 | + "text": [ |
| 1523 | + "This is a multiplication operation\n", |
| 1524 | + "16\n" |
| 1525 | + ] |
| 1526 | + } |
| 1527 | + ], |
| 1528 | + "source": [ |
| 1529 | + "final()" |
| 1530 | + ] |
| 1531 | + }, |
| 1532 | + { |
| 1533 | + "cell_type": "markdown", |
| 1534 | + "metadata": {}, |
| 1535 | + "source": [ |
| 1536 | + "**But where is `@`?**" |
| 1537 | + ] |
| 1538 | + }, |
| 1539 | + { |
| 1540 | + "cell_type": "code", |
| 1541 | + "execution_count": 86, |
| 1542 | + "metadata": {}, |
| 1543 | + "outputs": [], |
| 1544 | + "source": [ |
| 1545 | + "def a_new_decorator(a_func):\n", |
| 1546 | + "\n", |
| 1547 | + " def wrapTheFunction():\n", |
| 1548 | + " print(\"This is before executing a_func()\")\n", |
| 1549 | + "\n", |
| 1550 | + " a_func()\n", |
| 1551 | + "\n", |
| 1552 | + " print(\"Decorated!!!\")\n", |
| 1553 | + "\n", |
| 1554 | + " return wrapTheFunction\n" |
| 1555 | + ] |
| 1556 | + }, |
| 1557 | + { |
| 1558 | + "cell_type": "code", |
| 1559 | + "execution_count": 89, |
| 1560 | + "metadata": {}, |
| 1561 | + "outputs": [ |
| 1562 | + { |
| 1563 | + "name": "stdout", |
| 1564 | + "output_type": "stream", |
| 1565 | + "text": [ |
| 1566 | + "This is before executing a_func()\n", |
| 1567 | + "I am the function which needs some decoration.\n", |
| 1568 | + "Decorated!!!\n" |
| 1569 | + ] |
| 1570 | + } |
| 1571 | + ], |
| 1572 | + "source": [ |
| 1573 | + "@a_new_decorator\n", |
| 1574 | + "def a_function_requiring_decoration():\n", |
| 1575 | + " print(\"I am the function which needs some decoration.\")\n", |
| 1576 | + "\n", |
| 1577 | + "a_function_requiring_decoration()" |
| 1578 | + ] |
| 1579 | + }, |
| 1580 | + { |
| 1581 | + "cell_type": "code", |
| 1582 | + "execution_count": 90, |
| 1583 | + "metadata": {}, |
| 1584 | + "outputs": [], |
| 1585 | + "source": [ |
| 1586 | + "def doubler(a_func):\n", |
| 1587 | + "\n", |
| 1588 | + " def wrap_it():\n", |
| 1589 | + " a_func()\n", |
| 1590 | + " final = a_func()*2\n", |
| 1591 | + " print(final)\n", |
| 1592 | + "\n", |
| 1593 | + " return wrap_it" |
| 1594 | + ] |
| 1595 | + }, |
| 1596 | + { |
| 1597 | + "cell_type": "code", |
| 1598 | + "execution_count": 93, |
| 1599 | + "metadata": {}, |
| 1600 | + "outputs": [ |
| 1601 | + { |
| 1602 | + "name": "stdout", |
| 1603 | + "output_type": "stream", |
| 1604 | + "text": [ |
| 1605 | + "18\n" |
| 1606 | + ] |
| 1607 | + } |
| 1608 | + ], |
| 1609 | + "source": [ |
| 1610 | + "@doubler\n", |
| 1611 | + "def sum():\n", |
| 1612 | + " a = 4\n", |
| 1613 | + " b = 5\n", |
| 1614 | + " return a+b\n", |
| 1615 | + "sum()" |
| 1616 | + ] |
| 1617 | + }, |
| 1618 | + { |
| 1619 | + "cell_type": "code", |
| 1620 | + "execution_count": null, |
| 1621 | + "metadata": {}, |
| 1622 | + "outputs": [], |
| 1623 | + "source": [] |
1435 | 1624 | } |
1436 | 1625 | ], |
1437 | 1626 | "metadata": { |
|
0 commit comments