{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objekte und OO-Programmierung" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```py\n", "# Ausgangslage\n", "employees = {\n", " \"name 1\": {\n", " \"salary\": ...,\n", " \"skills\": [...],\n", " },\n", " \"name 2\": {\n", " \"salary\": ...,\n", " \"skills\": [...],\n", " },\n", " ...\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unknown\n", "0\n", "[]\n", "Unknown\n", "0\n", "[]\n" ] } ], "source": [ "class Employee:\n", " name = \"Unknown\"\n", " gehalt = 0\n", " skills = []\n", "\n", "employee1 = Employee()\n", "print(employee1.name)\n", "print(employee1.gehalt)\n", "print(employee1.skills)\n", "\n", "employee2 = Employee()\n", "print(employee2.name)\n", "print(employee2.gehalt)\n", "print(employee2.skills)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joe Doe\n", "Unknown\n" ] } ], "source": [ "employee1.name = \"Joe Doe\"\n", "print(employee1.name)\n", "print(employee2.name)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Init!\n" ] } ], "source": [ "class Employee:\n", " def __init__(self):\n", " print(\"Init!\")\n", " \n", "employee3 = Employee()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Joe Doe\n", "2000\n", "[]\n", "Jane Oh\n", "3000\n", "['marketing']\n" ] } ], "source": [ "class Employee:\n", " def __init__(self, name=\"\", gehalt=0, skills=[]):\n", " self.name = name\n", " self.gehalt = gehalt\n", "\n", " if skills is None:\n", " self.skills = []\n", " else:\n", " self.skills = skills\n", "\n", "employee4 = Employee(\"Joe Doe\", 2000, [])\n", "print(type(employee4))\n", "print(employee4.name)\n", "print(employee4.gehalt)\n", "print(employee4.skills)#\n", "\n", "employee5 = Employee(\"Jane Oh\", 3000, [\"marketing\"])\n", "print(employee5.name)\n", "print(employee5.gehalt)\n", "print(employee5.skills)\n", "\n", "# Geht nicht, weil die Klasse kein Attribut \"name\" definiert\n", "#print(Employee.name)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "0\n", "[]\n" ] } ], "source": [ "employee6 = Employee()\n", "print(employee6.name)\n", "print(employee6.gehalt)\n", "print(employee6.skills)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Employee 'Bob Smith'\n", " gehalt: 0\n", " skills:\n", " - software development\n", " - it support\n" ] } ], "source": [ "class Employee:\n", " def __init__(self, name=\"\", gehalt=0, skills=[]):\n", " self.name = name\n", " self.gehalt = gehalt\n", "\n", " if skills is None:\n", " self.skills = []\n", " else:\n", " self.skills = skills\n", "\n", " def __str__(self):\n", " s = [f\"Employee '{self.name}'\"]\n", " s.append(f\" gehalt: {self.gehalt}\")\n", " s.append(f\" skills:\")\n", " for skill in self.skills:\n", " s.append(f\" - {skill}\")\n", " return \"\\n\".join(s)\n", "\n", "\"\"\"\n", "Employee 'name':\n", " gehalt: XXX\n", " skills:\n", " - skill 1\n", " - skill 2\n", "\"\"\"\n", "employee7 = Employee(name=\"Bob Smith\", skills=[\"software development\", \"it support\"])\n", "print(employee7)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo, Welt\n", "Guten Tag, Welt\n", "Welt, Guten Tag\n", "Welt, Guten Tag\n", "Guten Tag, Welt\n" ] } ], "source": [ "# Exkurs: Strings und `format()`\n", "\n", "s = \"Hallo, {}\"\n", "print(s.format(\"Welt\"))\n", "\n", "s = \"{}, {}\"\n", "print(s.format(\"Guten Tag\", \"Welt\"))\n", "\n", "s = \"{name}, {grussformel}\"\n", "print(s.format(grussformel=\"Guten Tag\", name=\"Welt\"))\n", "\n", "grussformel=\"Guten Tag\"\n", "name=\"Welt\"\n", "print(f\"{name}, {grussformel}\")\n", "\n", "\n", "def sagehallo(name):\n", " # 'grussformel' ist eine globale variable!\n", " print(f\"{grussformel}, {name}\")\n", "\n", "sagehallo(\"Welt\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5000\n" ] }, { "ename": "ValueError", "evalue": "Zu hohes gehalt!", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/objects.ipynb Cell 10\u001b[0m line \u001b[0;36m2\n\u001b[1;32m 22\u001b[0m \u001b[39mprint\u001b[39m(employee8\u001b[39m.\u001b[39mgehalt)\n\u001b[1;32m 23\u001b[0m \u001b[39m# ValueError!\u001b[39;00m\n\u001b[0;32m---> 24\u001b[0m employee8\u001b[39m.\u001b[39;49mset_gehalt(\u001b[39m4000\u001b[39;49m)\n\u001b[1;32m 25\u001b[0m \u001b[39m# TypeError!\u001b[39;00m\n\u001b[1;32m 26\u001b[0m employee8\u001b[39m.\u001b[39mset_gehalt(\u001b[39m\"\u001b[39m\u001b[39mein sack mehl\u001b[39m\u001b[39m\"\u001b[39m)\n", "\u001b[1;32m/home/chris/work/python-kurs-softed/notebooks/objects.ipynb Cell 10\u001b[0m line \u001b[0;36m1\n\u001b[1;32m 15\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mgehalt \u001b[39m=\u001b[39m neues_gehalt\n\u001b[1;32m 16\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m---> 17\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mZu hohes gehalt!\u001b[39m\u001b[39m\"\u001b[39m)\n", "\u001b[0;31mValueError\u001b[0m: Zu hohes gehalt!" ] } ], "source": [ "class Employee:\n", " def __init__(self, name=\"\", gehalt=0, skills=[]):\n", " self.name = name\n", " self.gehalt = gehalt\n", "\n", " if skills is None:\n", " self.skills = []\n", " else:\n", " self.skills = skills\n", "\n", " def set_gehalt(self, neues_gehalt):\n", " if not isinstance(neues_gehalt, int):\n", " raise TypeError(\"gehalt muss ein Integer sein\")\n", " if neues_gehalt < 4000:\n", " self.gehalt = neues_gehalt\n", " else:\n", " raise ValueError(\"Zu hohes gehalt!\")\n", "\n", "employee8 = Employee()\n", "# Ok\n", "employee8.gehalt = 5000\n", "print(employee8.gehalt)\n", "# ValueError!\n", "employee8.set_gehalt(4000)\n", "# TypeError!\n", "employee8.set_gehalt(\"ein sack mehl\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "softed", "language": "python", "name": "softed" }, "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.11.8" } }, "nbformat": 4, "nbformat_minor": 2 }