|
26 | 26 | }, |
27 | 27 | { |
28 | 28 | "cell_type": "code", |
29 | | - "execution_count": 9, |
| 29 | + "execution_count": 10, |
30 | 30 | "id": "bdd215a1", |
31 | 31 | "metadata": {}, |
| 32 | + "outputs": [], |
| 33 | + "source": [ |
| 34 | + "class Car:\n", |
| 35 | + " def __init__(self, make, model, year):\n", |
| 36 | + " self.make = make\n", |
| 37 | + " self.model = model\n", |
| 38 | + " self.year = year\n", |
| 39 | + " \n", |
| 40 | + " def display(self):\n", |
| 41 | + " print(f\" {self.year} {self.make} {self.model}\")" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "id": "a9c324ce", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "* **__init__()** : is the instance initializer that runs automatically after a new object is created typically used to set up initial state (attributes) for that instance.\n", |
| 50 | + "* **self** : is the reference to the current object, passed as the first parameter to instance methods so you can access and modify the object attributes and call its other methods." |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "markdown", |
| 55 | + "id": "e84a14c4", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "## Declaring an instance of a class" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 11, |
| 64 | + "id": "25852529", |
| 65 | + "metadata": {}, |
32 | 66 | "outputs": [ |
33 | 67 | { |
34 | 68 | "name": "stdout", |
|
40 | 74 | } |
41 | 75 | ], |
42 | 76 | "source": [ |
43 | | - "class Car:\n", |
| 77 | + "car = Car(\"Volvo\", \"XC90\", \"2023\")\n", |
| 78 | + "car.display()\n", |
| 79 | + "car.model = \"XC60\"\n", |
| 80 | + "car.display()" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "id": "967aba95", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "## Inheritence" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 12, |
| 94 | + "id": "6ed40d08", |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [ |
| 97 | + { |
| 98 | + "name": "stdout", |
| 99 | + "output_type": "stream", |
| 100 | + "text": [ |
| 101 | + " 2023 Rivian R1S\n" |
| 102 | + ] |
| 103 | + } |
| 104 | + ], |
| 105 | + "source": [ |
| 106 | + "class ElectricCar(Car):\n", |
44 | 107 | " def __init__(self, make, model, year):\n", |
45 | | - " self.make = make\n", |
46 | | - " self.model = model\n", |
47 | | - " self.year = year\n", |
48 | | - " \n", |
49 | | - " def display(self):\n", |
50 | | - " print(f\" {self.year} {self.make} {self.model}\")\n", |
| 108 | + " super().__init__(make, model, year)\n", |
51 | 109 | "\n", |
52 | | - "if __name__ == '__main__':\n", |
53 | | - " car = Car(\"Volvo\", \"XC90\", \"2023\")\n", |
54 | | - " car.display()\n", |
55 | | - " car.model = \"XC60\"\n", |
56 | | - " car.display()" |
| 110 | + "car = ElectricCar(\"Rivian\", \"R1S\", \"2023\")\n", |
| 111 | + "car.display()" |
57 | 112 | ] |
58 | 113 | }, |
59 | 114 | { |
60 | 115 | "cell_type": "markdown", |
61 | | - "id": "a9c324ce", |
| 116 | + "id": "2e3e1c50", |
62 | 117 | "metadata": {}, |
63 | 118 | "source": [ |
64 | | - "* **__init__()** : is the instance initializer that runs automatically after a new object is created typically used to set up initial state (attributes) for that instance.\n", |
65 | | - "* **self** : is the reference to the current object, passed as the first parameter to instance methods so you can access and modify the object attributes and call its other methods." |
| 119 | + "* **super()**: is a built-in function in Python that provides access to methods in a parent or superclass from a child class. It's commonly used in inheritance to call methods from the parent class, especially in the __init__ method." |
66 | 120 | ] |
67 | 121 | } |
68 | 122 | ], |
|
0 commit comments