{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW03 - OOP I\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def get_random_color():\n", " return \"Blue\"\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class Car:\n", " \n", " def __init__(self, brand:str):\n", " \"\"\" \n", " Create a Car Object\n", "\n", " Parameters:\n", " brand (str): Brand of the car\n", "\n", " Returns:\n", " self Car Object\n", " \"\"\"\n", " \n", " self.brand = brand\n", " self.milage = 0\n", " self.color = get_random_color()\n", "\n", " def honk(self):\n", " print(f\"hupe\")\n", "\n", " def drive(self, distance:int)->None:\n", " \"\"\" \n", " Drives the car\n", "\n", " Parameters:\n", " distance (int) : Distance to drive the car\n", " \n", " Returns:\n", " None\n", " \"\"\"\n", " # interner aufruf (lesen & schreiben) von Attribut\n", " self.milage += distance\n", " # interner aufruf von Methode\n", " self.honk()\n", " print(f\"ich fahre {distance} km\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "c1 = Car(\"Volvo\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(\"test\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Car" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(c1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(c1) == Car" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(c1,Car)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Car object at 0x78d6ac375160>\n" ] } ], "source": [ "print(c1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(c1.milage)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'brand': 'Volvo', 'milage': 0, 'color': 'Blue'}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1.__dict__" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hupe\n", "ich fahre 50 km\n" ] } ], "source": [ "c1.drive(50)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n" ] } ], "source": [ "print(c1.milage)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000\n" ] } ], "source": [ "c1.milage = 10_000\n", "print(c1.milage)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1.milage" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ">" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1.drive" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Ford'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1.brand = \"Ford\"\n", "c1.brand" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hupe\n", "ich fahre 10 km\n" ] } ], "source": [ "c1.drive(10)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method drive in module __main__:\n", "\n", "drive(distance: int) -> None method of __main__.Car instance\n", " Drives the car\n", "\n", " Parameters:\n", " distance (int) : Distance to drive the car\n", "\n", " Returns:\n", " None\n", "\n" ] } ], "source": [ "help(c1.drive)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'c2' is not defined", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[19]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mc2\u001b[49m.drive(\u001b[32m20\u001b[39m)\n", "\u001b[31mNameError\u001b[39m: name 'c2' is not defined" ] } ], "source": [ "c2.drive(20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.Car at 0x13b01fee940>" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def get_random_zip()->int:\n", " \"\"\" Returns random int between 1000 and 9999\"\"\"\n", " return np.random.randint(1000,9999)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Person:\n", " \"\"\" \n", " Person Class\n", "\n", " Attributes:\n", " name (str) Name\n", " street (str) Street Name\n", " zip_city (int) ZIP of City\n", " age (int) Age of Person \n", " \"\"\"\n", "\n", " def __init__(self, name:str, age:int):\n", " \"\"\" \n", " Create a Person\n", "\n", " Parameters:\n", " name (str) Name of Person\n", "\n", " Returns\n", " self Object of Person\n", " \"\"\"\n", " self.name = name\n", " self.street = \"Bahnhofsstrasse\"\n", " self.zip_city = get_random_zip()\n", " self.age = age\n", "\n", " print(self)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Person object at 0x000001A77F4F22B0>\n" ] } ], "source": [ "p1 = Person(name = \"John\", age = 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Person object at 0x000001A77F4F2280>\n", "<__main__.Person object at 0x000001A77F461D30>\n" ] } ], "source": [ "p1 = Person(name = \"John\", age = 10)\n", "p2 = Person(\"Jack\", 55)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class Person in module __main__:\n", "\n", "class Person(builtins.object)\n", " | Person(name: str)\n", " | \n", " | Person Class\n", " | \n", " | Attributes:\n", " | name (str) Name\n", " | street (str) Street Name\n", " | zip_city (int) ZIP of City\n", " | age (int) Age of Person\n", " | \n", " | Methods defined here:\n", " | \n", " | __init__(self, name: str)\n", " | Create a Person\n", " | \n", " | Parameters:\n", " | name (str) Name of Person\n", " | \n", " | Returns\n", " | self Object of Person\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n" ] } ], "source": [ "help(Person)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'John'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1.name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1.age" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7310" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1.zip_city" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7159" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2.zip_city" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Copy by reference" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 10\n", "y = x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = 40" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40\n", "10\n" ] } ], "source": [ "print(y)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p3 = p1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Person object at 0x000001A77F4F2280>\n", "<__main__.Person object at 0x000001A77F4F2280>\n" ] } ], "source": [ "print(p1)\n", "print(p3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p1.name = \"Jane\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jane'" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p3.name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Copy by Value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from copy import copy" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p4 = copy(p1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Person object at 0x000001A77F4F2280>\n", "<__main__.Person object at 0x000001A77F4F21C0>\n" ] } ], "source": [ "print(p1)\n", "print(p4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jane\n", "Jane\n" ] } ], "source": [ "print(p1.name)\n", "print(p4.name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2227\n", "2227\n" ] } ], "source": [ "print(p1.zip_city)\n", "print(p4.zip_city)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Person object at 0x000001A77F5AB490>\n" ] } ], "source": [ "p5 = Person(\"Jane\", 55)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Getter & Setter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p1.age = -40" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-40" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1.age" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Animal:\n", "\n", " def __init__(self, age):\n", " self.__age = self.set_age(age)\n", "\n", " \n", " def set_age(self, age:int):\n", " \n", " if age < 0:\n", " print(f\"Age can not be < 0\")\n", " \n", " else:\n", " self.__age = age\n", " return age\n", " \n", "\n", " def get_age(self):\n", " return self.__age" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a1 = Animal(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Animal' object has no attribute '__age'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [99]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__age\u001b[49m\n", "\u001b[1;31mAttributeError\u001b[0m: 'Animal' object has no attribute '__age'" ] } ], "source": [ "a1.__age" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "50" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.set_age(50)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n" ] } ], "source": [ "print(a1.get_age())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a1.age = -50\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-50\n" ] } ], "source": [ "print(a1.age)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.set_age(30)\n", "a1.age" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Age can not be < 0\n" ] }, { "data": { "text/plain": [ "30" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.set_age(-30)\n", "a1.age" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Bank:\n", "\n", " def __init__(self, pin:int):\n", " self.__pin = pin\n", " self.__check_pin()\n", " \n", " def __check_pin(self):\n", "\n", " if self.__pin == 1234:\n", " raise ValueError(f\"Pin must not be {self.__pin}\")\n", "\n", " def authenticate(self, pin:str)->bool:\n", "\n", " return self.__pin == pin" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b1 = Bank(2345)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b1._Bank__check_pin()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Bank' object has no attribute '__pin'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [107]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mb1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pin\u001b[49m\n", "\u001b[1;31mAttributeError\u001b[0m: 'Bank' object has no attribute '__pin'" ] } ], "source": [ "b1.__pin" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b1.authenticate(1234)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Bank" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Bank' object has no attribute '__pin'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [111]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mb1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pin\u001b[49m\n", "\u001b[1;31mAttributeError\u001b[0m: 'Bank' object has no attribute '__pin'" ] } ], "source": [ "b1.__pin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dynamic Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b1.name = \"LUKB\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b1.name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Garbage Collection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20\n" ] } ], "source": [ "def add_10(x1:int):\n", " x2 = 10\n", " return x1 + x2\n", "\n", "y = add_10(10)\n", "print(y)\n", "\n", "\n", "# Free memory from Person object\n", "p1 = None\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'NoneType' object has no attribute 'name'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", "Input \u001b[1;32mIn [126]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mp1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\n", "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'name'" ] } ], "source": [ "p1.name" ] } ], "metadata": { "kernelspec": { "display_name": "HSLU_-_Python_Advanced-MB8bNCJM", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 2 }