Skip to content

Commit 902819f

Browse files
committed
Update classes note
1 parent 48ddfd6 commit 902819f

File tree

1 file changed

+70
-16
lines changed

1 file changed

+70
-16
lines changed

docs/python/language/classes.ipynb

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,43 @@
2626
},
2727
{
2828
"cell_type": "code",
29-
"execution_count": 9,
29+
"execution_count": 10,
3030
"id": "bdd215a1",
3131
"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": {},
3266
"outputs": [
3367
{
3468
"name": "stdout",
@@ -40,29 +74,49 @@
4074
}
4175
],
4276
"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",
44107
" 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",
51109
"\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()"
57112
]
58113
},
59114
{
60115
"cell_type": "markdown",
61-
"id": "a9c324ce",
116+
"id": "2e3e1c50",
62117
"metadata": {},
63118
"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."
66120
]
67121
}
68122
],

0 commit comments

Comments
 (0)