{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Classes\n", "===\n", "\n", "Classes sound and look complicated because you can use them in such a huge variety of ways. But at their heart, they just allow you to **combine information and behavior**.\n", "\n", "You can represent just about anything from the real world, both physical things and concepts." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example: let's represent a tank.\n", "\n", "class Tank:\n", " \"\"\"This will represent a tank in a game.\"\"\"\n", " \n", " def __init__(self):\n", " # Any function starting with two underscores is special in python.\n", " # This function runs automaticaly.\n", " # self refers to the tank that's being created.\n", " \n", " # All tanks start with armor.\n", " self.has_armor = True\n", " self.armor_strength = 5" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Has armor: True\n", "Roger's tank's armor has a strength of 5.\n" ] } ], "source": [ "# Let's make a tank!\n", "\n", "rogers_tank = Tank()\n", "\n", "print(f\"Has armor: {rogers_tank.has_armor}\")\n", "print(f\"Roger's tank's armor has a strength of {rogers_tank.armor_strength}.\")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Has armor: True\n", "Psalm's tank's armor has a strength of 8.\n" ] } ], "source": [ "# Let's make another tank!\n", "psalms_tank = Tank()\n", "psalms_tank.armor_strength = 8\n", "\n", "print(f\"Has armor: {psalms_tank.has_armor}\")\n", "print(f\"Psalm's tank's armor has a strength of {psalms_tank.armor_strength}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's add an owner." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example: let's represent a tank.\n", "\n", "class Tank:\n", " \"\"\"This will represent a tank in a game.\"\"\"\n", " \n", " def __init__(self):\n", " # Any function starting with two underscores is special in python.\n", " # This function runs automaticaly.\n", " # self refers to the tank that's being created.\n", " \n", " # All tanks start with armor.\n", " self.has_armor = True\n", " self.armor_strength = 5\n", " \n", " # Each tank needs an owner!\n", " self.owner = None" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Has armor: True\n", "roger's tank's armor has a strength of 5.\n" ] } ], "source": [ "# Let's make a tank!\n", "\n", "rogers_tank = Tank()\n", "rogers_tank.owner = 'roger'\n", "\n", "print(f\"Has armor: {rogers_tank.has_armor}\")\n", "print(f\"{rogers_tank.owner}'s tank's armor has a strength of {rogers_tank.armor_strength}.\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Has armor: True\n", "psalm's armor has a strength of 8.\n" ] } ], "source": [ "# Let's make another tank!\n", "psalms_tank = Tank()\n", "psalms_tank.armor_strength = 8\n", "psalms_tank.owner = 'psalm'\n", "\n", "print(f\"Has armor: {psalms_tank.has_armor}\")\n", "print(f\"{psalms_tank.owner}'s armor has a strength of {psalms_tank.armor_strength}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What about behavior?\n", "---\n", "\n", "You can add as many functions as you need to a class." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example: let's represent a tank.\n", "\n", "class BetterTank:\n", " \"\"\"This will represent a tank in a game.\"\"\"\n", " \n", " def __init__(self):\n", " # Any function starting with two underscores is special in python.\n", " # This function runs automaticaly.\n", " # self refers to the tank that's being created.\n", " \n", " # All tanks start with armor.\n", " self.has_armor = True\n", " self.armor_strength = 5\n", " \n", " # Each tank needs an owner!\n", " self.owner = None\n", " \n", " def shoot(self):\n", " \"\"\"This tank can shoot.\"\"\"\n", " # This is our own function, so it doesn't get double underscores.\n", " print(f\"{self.owner}'s tank is shooting.\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "phil's tank is shooting.\n" ] } ], "source": [ "phils_tank = BetterTank()\n", "phils_tank.owner = 'phil'\n", "phils_tank.shoot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An army of tanks!\n", "---\n", "\n", "Once you have a class defined, you can make as many objects as you want from that class. Let's make an army of tanks!" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eric's tank has 5 armor.\n", "psalm's tank has 5 armor.\n", "roger's tank has 5 armor.\n", "devin's tank has 5 armor.\n", "evan's tank has 5 armor.\n", "phil's tank has 5 armor.\n" ] } ], "source": [ "names = ['eric', 'psalm', 'roger', 'devin', 'evan', 'phil']\n", "\n", "# Make an empty list to store our tanks.\n", "tanks = []\n", "\n", "# Create a tank for every person.\n", "for name in names:\n", " new_tank = Tank()\n", " new_tank.owner = name\n", " \n", " tanks.append(new_tank)\n", " \n", "# Show that we've made an army of tanks.\n", "for tank in tanks:\n", " print(f\"{tank.owner}'s tank has {tank.armor_strength} armor.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's boring, everyone has the same tank!\n", "\n", "Let's give everyone a tank with random strength." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eric's tank has 8 armor.\n", "psalm's tank has 6 armor.\n", "roger's tank has 9 armor.\n", "devin's tank has 4 armor.\n", "evan's tank has 9 armor.\n", "phil's tank has 9 armor.\n", "abraham's tank has 0 armor.\n" ] } ], "source": [ "from random import randint\n", "\n", "names = ['eric', 'psalm', 'roger', 'devin', 'evan', 'phil', 'abraham']\n", "\n", "# Make an empty list to store our tanks.\n", "tanks = []\n", "\n", "# Create a tank for every person.\n", "for name in names:\n", " new_tank = Tank()\n", " new_tank.owner = name\n", " \n", " # Give everyone a random armor strength.\n", " new_tank.armor_strength = randint(0, 10)\n", " \n", " tanks.append(new_tank)\n", " \n", "# Show that we've made an army of tanks.\n", "for tank in tanks:\n", " print(f\"{tank.owner}'s tank has {tank.armor_strength} armor.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But psalm deserves the strongest tank!" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eric's tank has 5 armor.\n", "psalm's tank has 100 armor.\n", "roger's tank has 4 armor.\n", "devin's tank has 6 armor.\n", "evan's tank has 2 armor.\n", "phil's tank has 3 armor.\n", "abraham's tank has 10 armor.\n" ] } ], "source": [ "from random import randint\n", "\n", "names = ['eric', 'psalm', 'roger', 'devin', 'evan', 'phil', 'abraham']\n", "\n", "# Make an empty list to store our tanks.\n", "tanks = []\n", "\n", "# Create a tank for every person.\n", "for name in names:\n", " new_tank = Tank()\n", " new_tank.owner = name\n", " \n", " # Give everyone except psalm a random armor strength.\n", " if name == 'psalm':\n", " new_tank.armor_strength = 100\n", " else:\n", " new_tank.armor_strength = randint(0, 11)\n", " \n", " tanks.append(new_tank)\n", " \n", "# Show that we've made an army of tanks.\n", "for tank in tanks:\n", " print(f\"{tank.owner}'s tank has {tank.armor_strength} armor.\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }