|
733 | 733 | "id": "2b37956a",
|
734 | 734 | "metadata": {},
|
735 | 735 | "source": [
|
736 |
| - "### Property Decorator: A Pythonic Way to Use Getters and Setters" |
| 736 | + "### Ensure Data Integrity with Getters and Setters in Python" |
737 | 737 | ]
|
738 | 738 | },
|
739 | 739 | {
|
|
742 | 742 | "id": "1188b89a",
|
743 | 743 | "metadata": {},
|
744 | 744 | "source": [
|
745 |
| - "To define the behaviors that are executed when a class attribute is set, use the `property` decorator.\n", |
| 745 | + "In Python, the property decorator controls property access and modification through getters and setters.\n", |
746 | 746 | "\n",
|
747 |
| - "In the code below, the getter method gets the value of the `color` attribute and the setter method restricts the `color` attribute modification to string data types only. " |
| 747 | + "For example, in a `BankAccount` class, without getters and setters, the balance can be directly modified, potentially leading to an invalid state." |
748 | 748 | ]
|
749 | 749 | },
|
750 | 750 | {
|
751 | 751 | "cell_type": "code",
|
752 |
| - "execution_count": 9, |
753 |
| - "id": "c83e352f", |
754 |
| - "metadata": { |
755 |
| - "ExecuteTime": { |
756 |
| - "end_time": "2021-08-30T01:08:59.507066Z", |
757 |
| - "start_time": "2021-08-30T01:08:59.461065Z" |
758 |
| - } |
759 |
| - }, |
| 752 | + "execution_count": 5, |
| 753 | + "id": "c3d5a814", |
| 754 | + "metadata": {}, |
760 | 755 | "outputs": [
|
761 | 756 | {
|
762 |
| - "data": { |
763 |
| - "text/plain": [ |
764 |
| - "'red'" |
765 |
| - ] |
766 |
| - }, |
767 |
| - "execution_count": 9, |
768 |
| - "metadata": {}, |
769 |
| - "output_type": "execute_result" |
| 757 | + "name": "stdout", |
| 758 | + "output_type": "stream", |
| 759 | + "text": [ |
| 760 | + "100\n", |
| 761 | + "-50\n" |
| 762 | + ] |
770 | 763 | }
|
771 | 764 | ],
|
772 | 765 | "source": [
|
773 |
| - "class Fruit:\n", |
774 |
| - " def __init__(self, name: str, color: str):\n", |
775 |
| - " self.name = name\n", |
776 |
| - " self._color = color\n", |
777 |
| - "\n", |
778 |
| - " @property # getter method\n", |
779 |
| - " def color(self):\n", |
780 |
| - " return self._color\n", |
| 766 | + "class BankAccount:\n", |
| 767 | + " def __init__(self, initial_balance: float):\n", |
| 768 | + " self.balance = initial_balance\n", |
781 | 769 | "\n",
|
782 |
| - " @color.setter # setter method\n", |
783 |
| - " def color(self, value):\n", |
784 |
| - " print(\"Setting value of color...\")\n", |
785 |
| - " if isinstance(value, str):\n", |
786 |
| - " self._color = value\n", |
787 |
| - " else:\n", |
788 |
| - " raise AttributeError(\"Fruit's color must be a string.\")\n", |
| 770 | + "account = BankAccount(100)\n", |
| 771 | + "print(account.balance) \n", |
789 | 772 | "\n",
|
790 |
| - "\n", |
791 |
| - "fruit = Fruit(\"apple\", \"red\")\n", |
792 |
| - "fruit.color\n" |
| 773 | + "# Directly modifying the balance (no validation)\n", |
| 774 | + "account.balance = -50\n", |
| 775 | + "print(account.balance) " |
793 | 776 | ]
|
794 | 777 | },
|
795 | 778 | {
|
796 |
| - "cell_type": "code", |
797 |
| - "execution_count": 10, |
798 |
| - "id": "9fa947aa", |
799 |
| - "metadata": { |
800 |
| - "ExecuteTime": { |
801 |
| - "end_time": "2021-08-30T01:09:03.327029Z", |
802 |
| - "start_time": "2021-08-30T01:09:03.301356Z" |
803 |
| - } |
804 |
| - }, |
805 |
| - "outputs": [ |
806 |
| - { |
807 |
| - "name": "stdout", |
808 |
| - "output_type": "stream", |
809 |
| - "text": [ |
810 |
| - "Setting value of color...\n" |
811 |
| - ] |
812 |
| - }, |
813 |
| - { |
814 |
| - "data": { |
815 |
| - "text/plain": [ |
816 |
| - "'yellow'" |
817 |
| - ] |
818 |
| - }, |
819 |
| - "execution_count": 10, |
820 |
| - "metadata": {}, |
821 |
| - "output_type": "execute_result" |
822 |
| - } |
823 |
| - ], |
| 779 | + "cell_type": "markdown", |
| 780 | + "id": "8843dd71", |
| 781 | + "metadata": {}, |
824 | 782 | "source": [
|
825 |
| - "fruit.color = \"yellow\"\n", |
826 |
| - "fruit.color " |
| 783 | + "Using getters and setters ensures the balance cannot be set to an invalid value." |
827 | 784 | ]
|
828 | 785 | },
|
829 | 786 | {
|
830 | 787 | "cell_type": "code",
|
831 |
| - "execution_count": 11, |
832 |
| - "id": "1b2867b6", |
| 788 | + "execution_count": 6, |
| 789 | + "id": "ce4529d1", |
833 | 790 | "metadata": {},
|
834 | 791 | "outputs": [
|
835 | 792 | {
|
836 | 793 | "name": "stdout",
|
837 | 794 | "output_type": "stream",
|
838 | 795 | "text": [
|
839 |
| - "Setting value of color...\n" |
| 796 | + "100\n" |
840 | 797 | ]
|
841 | 798 | },
|
842 | 799 | {
|
843 |
| - "ename": "AttributeError", |
844 |
| - "evalue": "Fruit's color must be a string.", |
| 800 | + "ename": "ValueError", |
| 801 | + "evalue": "Balance cannot be negative", |
845 | 802 | "output_type": "error",
|
846 | 803 | "traceback": [
|
847 | 804 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
848 |
| - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", |
849 |
| - "\u001b[0;32m/var/folders/5w/fg65_rp17lz39z89p0nkv8ch0000gn/T/ipykernel_78260/1033431134.py\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfruit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
850 |
| - "\u001b[0;32m/var/folders/5w/fg65_rp17lz39z89p0nkv8ch0000gn/T/ipykernel_78260/3888926808.py\u001b[0m in \u001b[0;36mcolor\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_color\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Fruit's color must be a string.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
851 |
| - "\u001b[0;31mAttributeError\u001b[0m: Fruit's color must be a string." |
| 805 | + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", |
| 806 | + "Cell \u001b[0;32mIn[6], line 21\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[38;5;28mprint\u001b[39m(account\u001b[38;5;241m.\u001b[39mbalance) \n\u001b[1;32m 20\u001b[0m \u001b[38;5;66;03m# Attempting to set an invalid balance\u001b[39;00m\n\u001b[0;32m---> 21\u001b[0m \u001b[43maccount\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbalance\u001b[49m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m50\u001b[39m \n", |
| 807 | + "Cell \u001b[0;32mIn[6], line 12\u001b[0m, in \u001b[0;36mBankAccount.balance\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;129m@balance\u001b[39m\u001b[38;5;241m.\u001b[39msetter\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mbalance\u001b[39m(\u001b[38;5;28mself\u001b[39m, value):\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m value \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 12\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBalance cannot be negative\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_balance \u001b[38;5;241m=\u001b[39m value\n", |
| 808 | + "\u001b[0;31mValueError\u001b[0m: Balance cannot be negative" |
852 | 809 | ]
|
853 | 810 | }
|
854 | 811 | ],
|
855 | 812 | "source": [
|
856 |
| - "fruit.color = 1" |
| 813 | + "class BankAccount:\n", |
| 814 | + " def __init__(self, initial_balance: float):\n", |
| 815 | + " self._balance = initial_balance\n", |
| 816 | + "\n", |
| 817 | + " @property\n", |
| 818 | + " def balance(self):\n", |
| 819 | + " return self._balance\n", |
| 820 | + "\n", |
| 821 | + " @balance.setter\n", |
| 822 | + " def balance(self, value):\n", |
| 823 | + " if value < 0:\n", |
| 824 | + " raise ValueError(\"Balance cannot be negative\")\n", |
| 825 | + " self._balance = value\n", |
| 826 | + "\n", |
| 827 | + "\n", |
| 828 | + "# Example usage\n", |
| 829 | + "account = BankAccount(100)\n", |
| 830 | + "print(account.balance) \n", |
| 831 | + "\n", |
| 832 | + "# Attempting to set an invalid balance\n", |
| 833 | + "account.balance = -50 " |
857 | 834 | ]
|
858 | 835 | },
|
859 | 836 | {
|
|
0 commit comments