{
"metadata": {
"kernelspec": {
"display_name": "Pyolite",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
},
"colab": {
"name": "ObjectedOrientedProgramming1.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"source": [
"From the Hiplisch (2018) Python for Finance 2nd ed:\n",
"\n",
"Class\n",
"An abstract definition of a certain type of objects. For example, a human being.\n",
"\n",
"Object\n",
"An instance of a class. For example, Sandra.\n",
"\n",
"Attribute\n",
"A feature of the class (class attribute) or of an instance of the class (instance attribute). For example, being a mammal, being male or female, or color of the eyes.\n",
"\n",
"Method\n",
"An operation that the class or an instance of the class can implement. For example, walking.\n",
"\n",
"Parameters\n",
"Input taken by a method to influence its behavior. For example, three steps.\n",
"\n",
"Instantiation\n",
"The process of creating a specific object based on an abstract class."
],
"metadata": {
"id": "b89412ee-83d3-499e-bebc-d87847cbe73c"
},
"id": "b89412ee-83d3-499e-bebc-d87847cbe73c"
},
{
"cell_type": "markdown",
"source": [
"Source: https://betterprogramming.pub/python-how-object-and-class-attributes-work-8edf4ed9caa4"
],
"metadata": {
"id": "4eee6490-17d9-4b52-ba58-07571c58450c"
},
"id": "4eee6490-17d9-4b52-ba58-07571c58450c"
},
{
"cell_type": "code",
"source": [
"class Person:\n",
" class_attr = \"I am a class attribute\"\n",
" \n",
" def __init__(self, name=''):\n",
" self.name = name"
],
"metadata": {
"trusted": true,
"id": "514f053a-8846-4c20-80e6-67b172d73770"
},
"execution_count": null,
"outputs": [],
"id": "514f053a-8846-4c20-80e6-67b172d73770"
},
{
"cell_type": "code",
"source": [
"person_1 = Person('Laura')"
],
"metadata": {
"trusted": true,
"id": "69dfc110-6472-49b2-8cfc-6cebce589b30"
},
"execution_count": null,
"outputs": [],
"id": "69dfc110-6472-49b2-8cfc-6cebce589b30"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "11588f2f-cbf5-40de-bc05-800e474c3479"
},
"execution_count": null,
"outputs": [],
"id": "11588f2f-cbf5-40de-bc05-800e474c3479"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "5d80cad4-94ba-4381-8607-fed32e1d809c"
},
"execution_count": null,
"outputs": [],
"id": "5d80cad4-94ba-4381-8607-fed32e1d809c"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "adde9819-8334-4be1-a94b-c8d3b24aedc8"
},
"execution_count": null,
"outputs": [],
"id": "adde9819-8334-4be1-a94b-c8d3b24aedc8"
},
{
"cell_type": "code",
"source": [
"person_1.name"
],
"metadata": {
"trusted": true,
"id": "e09cb430-7a9c-462a-ac2f-45a06bcf2f74",
"outputId": "3808d04e-d066-4b1c-8871-747b3793cafa"
},
"execution_count": null,
"outputs": [
{
"execution_count": 7,
"output_type": "execute_result",
"data": {
"text/plain": "'Laura'"
},
"metadata": {}
}
],
"id": "e09cb430-7a9c-462a-ac2f-45a06bcf2f74"
},
{
"cell_type": "code",
"source": [
"person_1.class_attr"
],
"metadata": {
"trusted": true,
"id": "02ccdaeb-5e7b-4b84-b454-26c56f5776b3",
"outputId": "8fb3f5c1-d0c5-4e75-cc9c-fafb3cafdb24"
},
"execution_count": null,
"outputs": [
{
"execution_count": 10,
"output_type": "execute_result",
"data": {
"text/plain": "'I am a class attribute'"
},
"metadata": {}
}
],
"id": "02ccdaeb-5e7b-4b84-b454-26c56f5776b3"
},
{
"cell_type": "code",
"source": [
"class Person:\n",
" class_attr = \"I am a class attribute\"\n",
" \n",
" def __init__(self, name=''):\n",
" self.name = name\n",
" \n",
" @property\n",
" def name(self):\n",
" return self.__name\n",
" @name.setter\n",
" def name(self, value):\n",
" if type(value) is not str:\n",
" raise TypeError(\"name must be a string\")\n",
" self.__name = value"
],
"metadata": {
"trusted": true,
"id": "d3d2f5ae-625a-4ffc-a128-701c9ba9431f"
},
"execution_count": null,
"outputs": [],
"id": "d3d2f5ae-625a-4ffc-a128-701c9ba9431f"
},
{
"cell_type": "code",
"source": [
"person_1 = Person('Laura')\n",
"print(person_1.name)"
],
"metadata": {
"trusted": true,
"id": "f739131a-4a07-4426-8904-44cf5c4b14cb",
"outputId": "f6427b98-622c-45b3-b25d-7d39de2b5b96"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "Laura\n",
"output_type": "stream"
}
],
"id": "f739131a-4a07-4426-8904-44cf5c4b14cb"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "57c03f99-0f83-444a-bede-4c8d8d2e29f7"
},
"execution_count": null,
"outputs": [],
"id": "57c03f99-0f83-444a-bede-4c8d8d2e29f7"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "83ed0aa3-1683-474d-a044-ca03b20fd124"
},
"execution_count": null,
"outputs": [],
"id": "83ed0aa3-1683-474d-a044-ca03b20fd124"
},
{
"cell_type": "code",
"source": [
"class HumanBeing(object): \n",
" def __init__(self, first_name, eye_color): \n",
" self.first_name = first_name \n",
" self.eye_color = eye_color \n",
" self.position = 0 \n",
" def walk_steps(self, steps): \n",
" self.position += steps \n",
" "
],
"metadata": {
"trusted": true,
"id": "500aa25d-cd19-4d88-a83f-390ba2cc5157"
},
"execution_count": null,
"outputs": [],
"id": "500aa25d-cd19-4d88-a83f-390ba2cc5157"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "052cfd29-35e2-4967-aed0-13e73eafcb67"
},
"execution_count": null,
"outputs": [],
"id": "052cfd29-35e2-4967-aed0-13e73eafcb67"
},
{
"cell_type": "markdown",
"source": [
"From https://vegibit.com/python-class-examples/\n",
"\n",
"class\n",
"A user-defined compound type. A class can also be thought of as a template for the objects that are instances of it. (The iPhone is a class. By December 2010, estimates are that 50 million instances had been sold!)"
],
"metadata": {
"id": "ab79cf1c-d778-4b62-9799-5179e0585370"
},
"id": "ab79cf1c-d778-4b62-9799-5179e0585370"
},
{
"cell_type": "code",
"source": [
"class Point:\n",
" \"\"\" Create a new Point, at coordinates x, y \"\"\"\n",
"\n",
" def __init__(self, x=0, y=0):\n",
" \"\"\" Create a new point at x, y \"\"\"\n",
" self.x = x\n",
" self.y = y\n",
"\n",
" def addition(self):\n",
" \"\"\" Compute my distance from the origin \"\"\"\n",
" return self.x + self.y"
],
"metadata": {
"trusted": true,
"id": "8e921344-a79f-4a46-941c-9d644ae27d4a"
},
"execution_count": null,
"outputs": [],
"id": "8e921344-a79f-4a46-941c-9d644ae27d4a"
},
{
"cell_type": "code",
"source": [
"p = Point(1, 4)\n",
"print(p)"
],
"metadata": {
"trusted": true,
"id": "2866a8b4-afcc-4ed8-89df-ada8afe9a24e",
"outputId": "35ade279-a251-4f69-8ed5-4def3fabad73"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "<__main__.Point object at 0x39385f8>\n",
"output_type": "stream"
}
],
"id": "2866a8b4-afcc-4ed8-89df-ada8afe9a24e"
},
{
"cell_type": "code",
"source": [
"p.x"
],
"metadata": {
"trusted": true,
"id": "b8c8b01c-d87a-4da6-a927-be0afa29a05f",
"outputId": "3851f115-c501-4a2f-b954-2c41ab363da2"
},
"execution_count": null,
"outputs": [
{
"execution_count": 35,
"output_type": "execute_result",
"data": {
"text/plain": "1"
},
"metadata": {}
}
],
"id": "b8c8b01c-d87a-4da6-a927-be0afa29a05f"
},
{
"cell_type": "code",
"source": [
"p.y"
],
"metadata": {
"trusted": true,
"id": "557811e4-4ed1-4175-89f5-e9922d0bb5ba",
"outputId": "4db4c18e-ca2f-4593-ef81-f07b268009bb"
},
"execution_count": null,
"outputs": [
{
"execution_count": 36,
"output_type": "execute_result",
"data": {
"text/plain": "4"
},
"metadata": {}
}
],
"id": "557811e4-4ed1-4175-89f5-e9922d0bb5ba"
},
{
"cell_type": "code",
"source": [
"p.addition()"
],
"metadata": {
"trusted": true,
"id": "5198651c-d9ac-4222-a663-35e3ab748cbd",
"outputId": "17f5d050-d0ff-4e85-a288-c4e1f692990c"
},
"execution_count": null,
"outputs": [
{
"execution_count": 37,
"output_type": "execute_result",
"data": {
"text/plain": "5"
},
"metadata": {}
}
],
"id": "5198651c-d9ac-4222-a663-35e3ab748cbd"
},
{
"cell_type": "code",
"source": [
"q = Point(100, 2**3)"
],
"metadata": {
"trusted": true,
"id": "853779e4-70d1-4fbb-9050-aec408e08de4"
},
"execution_count": null,
"outputs": [],
"id": "853779e4-70d1-4fbb-9050-aec408e08de4"
},
{
"cell_type": "code",
"source": [
"q.addition()"
],
"metadata": {
"trusted": true,
"id": "ea53f02d-a20d-4633-adcb-bc4c55a80f49",
"outputId": "cf1d9590-76a8-4ebb-9c28-1005824c9807"
},
"execution_count": null,
"outputs": [
{
"execution_count": 41,
"output_type": "execute_result",
"data": {
"text/plain": "108"
},
"metadata": {}
}
],
"id": "ea53f02d-a20d-4633-adcb-bc4c55a80f49"
},
{
"cell_type": "code",
"source": [
"a = p.addition()*2 + q.addition()"
],
"metadata": {
"trusted": true,
"id": "8e4deda3-8842-4ae7-adc2-50c19f53a63c"
},
"execution_count": null,
"outputs": [],
"id": "8e4deda3-8842-4ae7-adc2-50c19f53a63c"
},
{
"cell_type": "code",
"source": [
"print(a)"
],
"metadata": {
"trusted": true,
"id": "cc018215-c706-4c91-af4c-8f2c34097ddf",
"outputId": "c817008c-fe58-4a55-b625-574456a9043a"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "118\n",
"output_type": "stream"
}
],
"id": "cc018215-c706-4c91-af4c-8f2c34097ddf"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "faaf50eb-49e7-4bd7-ad9a-1020072ed690"
},
"execution_count": null,
"outputs": [],
"id": "faaf50eb-49e7-4bd7-ad9a-1020072ed690"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "a92d3142-2262-4b89-8b36-86649191eb0d"
},
"execution_count": null,
"outputs": [],
"id": "a92d3142-2262-4b89-8b36-86649191eb0d"
},
{
"cell_type": "markdown",
"source": [
"# source: https://vegibit.com/python-class-examples/"
],
"metadata": {
"id": "39274e6a-1042-4ba9-948f-d2588580245b"
},
"id": "39274e6a-1042-4ba9-948f-d2588580245b"
},
{
"cell_type": "code",
"source": [
"class Vehicle:\n",
" def __init__(self, brand, model, type):\n",
" self.brand = brand\n",
" self.model = model\n",
" self.type = type\n",
" self.gas_tank_size = 14\n",
" self.fuel_level = 0\n",
"\n",
" def fuel_up(self):\n",
" self.fuel_level = self.gas_tank_size\n",
" print('Gas tank is now full.')\n",
"\n",
" def drive(self):\n",
" print(f'The {self.model} is now driving.')"
],
"metadata": {
"trusted": true,
"id": "eb84556a-1ea6-40dc-a710-87eeb58c3074"
},
"execution_count": null,
"outputs": [],
"id": "eb84556a-1ea6-40dc-a710-87eeb58c3074"
},
{
"cell_type": "code",
"source": [
"vehicle_object = Vehicle('Honda', 'Ridgeline', 'Truck')\n",
"a_subaru = Vehicle('Subaru', 'Forester', 'Crossover')\n",
"an_suv = Vehicle('Ford', 'Explorer', 'SUV')\n",
"\n",
"print(vehicle_object)\n",
"print(vehicle_object.brand)\n",
"print(vehicle_object.model)\n",
"print(vehicle_object.type)\n",
"\n",
"vehicle_object.fuel_up()\n",
"vehicle_object.drive()"
],
"metadata": {
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1dad15b7-004a-4095-8855-854d545c6fb4",
"outputId": "770fb442-40a3-4753-b83e-5dad6fed5c66"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"<__main__.Vehicle object at 0x7f23bf420bd0>\n",
"Honda\n",
"Ridgeline\n",
"Truck\n",
"Gas tank is now full.\n",
"The Ridgeline is now driving.\n"
]
}
],
"id": "1dad15b7-004a-4095-8855-854d545c6fb4"
},
{
"cell_type": "code",
"source": [
"cool_new_vehicle = Vehicle('Honda', 'Ridgeline', 'Truck')\n",
"cool_new_vehicle.fuel_level = 7"
],
"metadata": {
"trusted": true,
"id": "28db246b-d5ce-4298-9870-6b5edc0dde46"
},
"execution_count": null,
"outputs": [],
"id": "28db246b-d5ce-4298-9870-6b5edc0dde46"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "16bd40b9-9504-4383-bf0e-8e2dc0751f93"
},
"execution_count": null,
"outputs": [],
"id": "16bd40b9-9504-4383-bf0e-8e2dc0751f93"
},
{
"cell_type": "code",
"source": [
"class Vehicle:\n",
" def __init__(self, brand, model, type):\n",
" self.brand = brand\n",
" self.model = model\n",
" self.type = type\n",
" self.gas_tank_size = 14\n",
" self.fuel_level = 0\n",
"\n",
" def fuel_up(self):\n",
" self.fuel_level = self.gas_tank_size\n",
" print('Gas tank is now full.')\n",
"\n",
" def drive(self):\n",
" print(f'The {self.model} is now driving.')\n",
"\n",
" def update_fuel_level(self, new_level):\n",
" if new_level <= self.gas_tank_size:\n",
" self.fuel_level = new_level\n",
" else:\n",
" print('Exceeded capacity')"
],
"metadata": {
"trusted": true,
"id": "9caacf5e-c445-473f-af17-e897f0b12e90"
},
"execution_count": null,
"outputs": [],
"id": "9caacf5e-c445-473f-af17-e897f0b12e90"
},
{
"cell_type": "code",
"source": [
"class Vehicle:\n",
" def __init__(self, brand, model, type):\n",
" self.brand = brand\n",
" self.model = model\n",
" self.type = type\n",
" self.gas_tank_size = 14\n",
" self.fuel_level = 0\n",
"\n",
" def fuel_up(self):\n",
" self.fuel_level = self.gas_tank_size\n",
" print('Gas tank is now full.')\n",
"\n",
" def drive(self):\n",
" print(f'The {self.model} is now driving.')\n",
"\n",
" def update_fuel_level(self, new_level):\n",
" if new_level <= self.gas_tank_size:\n",
" self.fuel_level = new_level\n",
" else:\n",
" print('Exceeded capacity')\n",
"\n",
" def get_gas(self, amount):\n",
" if (self.fuel_level + amount <= self.gas_tank_size):\n",
" self.fuel_level += amount\n",
" print('Added fuel.')\n",
" else:\n",
" print('The tank wont hold that much.')"
],
"metadata": {
"trusted": true,
"id": "31f5e614-33b0-4f19-bfe5-256fcd8871b2"
},
"execution_count": null,
"outputs": [],
"id": "31f5e614-33b0-4f19-bfe5-256fcd8871b2"
},
{
"cell_type": "code",
"source": [
"class ElectricVehicle(Vehicle):\n",
" def __init__(self, brand, model, type):\n",
" super().__init__(brand, model, type)\n",
" self.battery_size = 85\n",
" self.charge_level = 0"
],
"metadata": {
"trusted": true,
"id": "cc320a3e-592e-43eb-bb4d-3d7f86c9cb3a"
},
"execution_count": null,
"outputs": [],
"id": "cc320a3e-592e-43eb-bb4d-3d7f86c9cb3a"
},
{
"cell_type": "code",
"source": [
"class ElectricVehicle(Vehicle):\n",
" def __init__(self, brand, model, type):\n",
" super().__init__(brand, model, type)\n",
" self.battery_size = 85\n",
" self.charge_level = 0\n",
"\n",
" def charge(self):\n",
" self.charge_level = 100\n",
" print('The vehicle is now charged.')\n",
"\n",
" def fuel_up(self):\n",
" print('This vehicle has no fuel tank!')"
],
"metadata": {
"trusted": true,
"id": "3807641e-d458-4c36-9ca0-6d03e3fb140a"
},
"execution_count": null,
"outputs": [],
"id": "3807641e-d458-4c36-9ca0-6d03e3fb140a"
},
{
"cell_type": "code",
"source": [
"electric_vehicle = ElectricVehicle('Tesla', 'Model 3', 'Car')\n",
"electric_vehicle.charge()\n",
"electric_vehicle.drive()"
],
"metadata": {
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "c61d26a8-a815-4145-ac32-58c0e83fe85f",
"outputId": "59dc099c-a2a0-4ba8-d09b-c21e9c148d3a"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The vehicle is now charged.\n",
"The Model 3 is now driving.\n"
]
}
],
"id": "c61d26a8-a815-4145-ac32-58c0e83fe85f"
},
{
"cell_type": "code",
"source": [
"class ElectricVehicle(Vehicle):\n",
" def __init__(self, brand, model, type):\n",
" super().__init__(brand, model, type)\n",
" self.battery_size = 85\n",
" self.charge_level = 0\n",
"\n",
" def charge(self):\n",
" self.charge_level = 100\n",
" print('The vehicle is now charged.')\n",
"\n",
" def fuel_up(self):\n",
" print('This vehicle has no fuel tank!')"
],
"metadata": {
"trusted": true,
"id": "81956a44-320b-4552-8f9a-b3605002396e"
},
"execution_count": null,
"outputs": [],
"id": "81956a44-320b-4552-8f9a-b3605002396e"
},
{
"cell_type": "code",
"source": [
"class Battery:\n",
" def __init__(self, size=85):\n",
" self.size = size\n",
" self.charge_level = 0\n",
"\n",
" def get_range(self):\n",
" if self.size == 85:\n",
" return 260\n",
" elif self.size == 100:\n",
" return 315"
],
"metadata": {
"trusted": true,
"id": "679b021a-f870-472e-b8b2-da434087f381"
},
"execution_count": null,
"outputs": [],
"id": "679b021a-f870-472e-b8b2-da434087f381"
},
{
"cell_type": "code",
"source": [
"class ElectricVehicle(Vehicle):\n",
" def __init__(self, brand, model, type):\n",
" super().__init__(brand, model, type)\n",
" self.battery = Battery()\n",
"\n",
" def charge(self):\n",
" self.battery.charge_level = 100\n",
"\n",
" print('The vehicle is fully charged.')"
],
"metadata": {
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "94ad414d-0717-4f7d-9720-d24b799875fa",
"outputId": "270483ac-47ae-4841-efbe-a8d386aa134e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The vehicle is fully charged.\n"
]
}
],
"id": "94ad414d-0717-4f7d-9720-d24b799875fa"
},
{
"cell_type": "code",
"source": [
"electric_vehicle = ElectricVehicle('Tesla', 'CyberTruck', 'Truck')\n",
"electric_vehicle.charge()\n",
"print(electric_vehicle.battery.get_range())\n",
"electric_vehicle.drive()"
],
"metadata": {
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "07eaa96a-3941-4846-aa71-f4c0ed7f71cf",
"outputId": "a3abcb2a-3a4a-417e-b426-9f6e11a5e04e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"260\n",
"The CyberTruck is now driving.\n"
]
}
],
"id": "07eaa96a-3941-4846-aa71-f4c0ed7f71cf"
},
{
"cell_type": "code",
"source": [
"class Vehicle:\n",
" \"\"\"Vehicle Class data and methods\"\"\"\n",
"\n",
"class Battery:\n",
" \"\"\"Batter Class data and methods\"\"\"\n",
"\n",
"class ElectricVehicle(Vehicle):\n",
" \"\"\"ElectricVehicle Class data and methods\"\"\""
],
"metadata": {
"trusted": true,
"id": "d0841494-aeb1-4a0a-add3-002c57a5956d"
},
"execution_count": null,
"outputs": [],
"id": "d0841494-aeb1-4a0a-add3-002c57a5956d"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "dd2a0c77-b774-4b66-8ff1-0b6b3f246823"
},
"execution_count": null,
"outputs": [],
"id": "dd2a0c77-b774-4b66-8ff1-0b6b3f246823"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "b24318aa-6cc0-4b30-acc6-6eab59a51386"
},
"execution_count": null,
"outputs": [],
"id": "b24318aa-6cc0-4b30-acc6-6eab59a51386"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "7013d0e6-2021-46cb-b9ac-0cef38ad5110"
},
"execution_count": null,
"outputs": [],
"id": "7013d0e6-2021-46cb-b9ac-0cef38ad5110"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "5482c18b-699e-4f88-8a83-9386dcaca4e8"
},
"execution_count": null,
"outputs": [],
"id": "5482c18b-699e-4f88-8a83-9386dcaca4e8"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "3c146dbc-cc18-42c8-8ded-07903eb56f41"
},
"execution_count": null,
"outputs": [],
"id": "3c146dbc-cc18-42c8-8ded-07903eb56f41"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "61a7ad64-56f8-460c-84f0-b16657811abd"
},
"execution_count": null,
"outputs": [],
"id": "61a7ad64-56f8-460c-84f0-b16657811abd"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "77d0590b-2d0b-4b47-b07b-12c9e61a6e7b"
},
"execution_count": null,
"outputs": [],
"id": "77d0590b-2d0b-4b47-b07b-12c9e61a6e7b"
},
{
"cell_type": "markdown",
"source": [
"# Source: https://www.digitalocean.com/community/tutorials/how-to-construct-classes-and-define-objects-in-python-3"
],
"metadata": {
"id": "42e67b1a-64ee-49f4-a07b-b9e0917ba43f"
},
"id": "42e67b1a-64ee-49f4-a07b-b9e0917ba43f"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def swim(self):\n",
" print(\"The shark is swimming.\")\n",
"\n",
" def be_awesome(self):\n",
" print(\"The shark is being awesome.\")"
],
"metadata": {
"trusted": true,
"id": "bcf5bad1-a876-425f-905d-9861105df3eb"
},
"execution_count": null,
"outputs": [],
"id": "bcf5bad1-a876-425f-905d-9861105df3eb"
},
{
"cell_type": "code",
"source": [
"sammy = Shark()"
],
"metadata": {
"trusted": true,
"id": "c7377630-1c35-4115-9c84-64938a552d16"
},
"execution_count": null,
"outputs": [],
"id": "c7377630-1c35-4115-9c84-64938a552d16"
},
{
"cell_type": "code",
"source": [
"sammy = Shark()\n",
"sammy.swim()\n",
"sammy.be_awesome()"
],
"metadata": {
"trusted": true,
"id": "e05fc700-c032-412d-aa40-e8467106bf25",
"outputId": "d7852383-680a-447c-e504-9cdc7e83b96f"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "The shark is swimming.\nThe shark is being awesome.\n",
"output_type": "stream"
}
],
"id": "e05fc700-c032-412d-aa40-e8467106bf25"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def swim(self):\n",
" print(\"The shark is swimming.\")\n",
"\n",
" def be_awesome(self):\n",
" print(\"The shark is being awesome.\")\n",
"\n",
"\n",
"def main():\n",
" sammy = Shark()\n",
" sammy.swim()\n",
" sammy.be_awesome()\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"metadata": {
"trusted": true,
"id": "d6d1805f-df65-4e7f-9e94-872d839f82f4",
"outputId": "7a3fa0da-4f9f-4df0-dfb5-49877652eff8"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "The shark is swimming.\nThe shark is being awesome.\n",
"output_type": "stream"
}
],
"id": "d6d1805f-df65-4e7f-9e94-872d839f82f4"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def __init__(self):\n",
" print(\"This is the constructor method.\")"
],
"metadata": {
"trusted": true,
"id": "ee93d04d-d18b-43c2-bbbc-e98132dcc7f1"
},
"execution_count": null,
"outputs": [],
"id": "ee93d04d-d18b-43c2-bbbc-e98132dcc7f1"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def __init__(self, name):\n",
" self.name = name"
],
"metadata": {
"trusted": true,
"id": "7654b818-5a9a-4d75-a121-453942fd102e"
},
"execution_count": null,
"outputs": [],
"id": "7654b818-5a9a-4d75-a121-453942fd102e"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def swim(self):\n",
" # Reference the name\n",
" print(self.name + \" is swimming.\")\n",
"\n",
" def be_awesome(self):\n",
" # Reference the name\n",
" print(self.name + \" is being awesome.\")"
],
"metadata": {
"trusted": true,
"id": "e203b305-7960-4d4a-8841-be4868e90432"
},
"execution_count": null,
"outputs": [],
"id": "e203b305-7960-4d4a-8841-be4868e90432"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def swim(self):\n",
" print(self.name + \" is swimming.\")\n",
"\n",
" def be_awesome(self):\n",
" print(self.name + \" is being awesome.\")\n",
"\n",
"\n",
"def main():\n",
" # Set name of Shark object\n",
" sammy = Shark(\"Sammy\")\n",
" sammy.swim()\n",
" sammy.be_awesome()\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"metadata": {
"trusted": true,
"id": "09066ce4-c834-479a-ac93-59a4e9f897ec",
"outputId": "e6c34b02-306c-4500-fa68-f43a2451c4b4"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "Sammy is swimming.\nSammy is being awesome.\n",
"output_type": "stream"
}
],
"id": "09066ce4-c834-479a-ac93-59a4e9f897ec"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def __init__(self, name, age):\n",
" self.name = name\n",
" self.age = age"
],
"metadata": {
"trusted": true,
"id": "d022bd88-2c4e-484b-b2d9-b98668a8a9c1"
},
"execution_count": null,
"outputs": [],
"id": "d022bd88-2c4e-484b-b2d9-b98668a8a9c1"
},
{
"cell_type": "code",
"source": [
"sammy = Shark(\"Sammy\", 5)"
],
"metadata": {
"trusted": true,
"id": "9ea39111-16cf-49cb-995a-d6d4210c8e1e"
},
"execution_count": null,
"outputs": [],
"id": "9ea39111-16cf-49cb-995a-d6d4210c8e1e"
},
{
"cell_type": "code",
"source": [
"class Shark:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
" def swim(self):\n",
" print(self.name + \" is swimming.\")\n",
"\n",
" def be_awesome(self):\n",
" print(self.name + \" is being awesome.\")\n",
"\n",
"def main():\n",
" sammy = Shark(\"Sammy\")\n",
" sammy.be_awesome()\n",
" stevie = Shark(\"Stevie\")\n",
" stevie.swim()\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"metadata": {
"trusted": true,
"id": "85b21188-cb4e-4b05-8adb-c939caf32bc8",
"outputId": "c864a384-163d-48ce-bab7-41561829ccd1"
},
"execution_count": null,
"outputs": [
{
"name": "stdout",
"text": "Sammy is being awesome.\nStevie is swimming.\n",
"output_type": "stream"
}
],
"id": "85b21188-cb4e-4b05-8adb-c939caf32bc8"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "7f03c52c-c8e2-4048-bd25-8009c8f36e84"
},
"execution_count": null,
"outputs": [],
"id": "7f03c52c-c8e2-4048-bd25-8009c8f36e84"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "f4bf731a-cb03-4a62-bad6-9dfee3cafd7d"
},
"execution_count": null,
"outputs": [],
"id": "f4bf731a-cb03-4a62-bad6-9dfee3cafd7d"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "8c6fa784-6cb3-4c74-8c81-ac1859f6ad27"
},
"execution_count": null,
"outputs": [],
"id": "8c6fa784-6cb3-4c74-8c81-ac1859f6ad27"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "50acec73-8f30-47e7-a37a-cd2ba238c51d"
},
"execution_count": null,
"outputs": [],
"id": "50acec73-8f30-47e7-a37a-cd2ba238c51d"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "7ec4588c-e1c2-46f9-9bf0-6c26803f2d59"
},
"execution_count": null,
"outputs": [],
"id": "7ec4588c-e1c2-46f9-9bf0-6c26803f2d59"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "57cc2c94-c4b5-415f-ba55-a6ab23e10976"
},
"execution_count": null,
"outputs": [],
"id": "57cc2c94-c4b5-415f-ba55-a6ab23e10976"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "642c69d7-b10d-4e52-89ea-511360eff094"
},
"execution_count": null,
"outputs": [],
"id": "642c69d7-b10d-4e52-89ea-511360eff094"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "965b04c9-ba01-4a12-95c6-2c86b86c3a0b"
},
"execution_count": null,
"outputs": [],
"id": "965b04c9-ba01-4a12-95c6-2c86b86c3a0b"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "0a1eb9e2-a657-48e7-a4f1-09a34c7c896b"
},
"execution_count": null,
"outputs": [],
"id": "0a1eb9e2-a657-48e7-a4f1-09a34c7c896b"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "3ca719f1-323d-4000-b73c-14a0bbf3eb27"
},
"execution_count": null,
"outputs": [],
"id": "3ca719f1-323d-4000-b73c-14a0bbf3eb27"
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "59e94c42-59cc-40f8-8f02-5e2ac141a754"
},
"execution_count": null,
"outputs": [],
"id": "59e94c42-59cc-40f8-8f02-5e2ac141a754"
}
]
}