{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You are already familiar with defining your own functions, like this:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def f(a):\n", " return a" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But what about defining your own types? First, lets remind ourselves of some built-in types:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "a = 2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, so there is a thing called an `int`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can make a new `int` by calling the type name as a function:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To build our own type, we can use the `class` keyword:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "class MyClass:\n", " pass" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.MyClass" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MyClass" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "instance = MyClass()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.MyClass" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(instance)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is this good for?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The essence of object-oriented programming is allowing for data and algorithms to be combined in one place. Functions allow us to re-use algorithms, but classes allow us to combine them with local data:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "class MyClass2:\n", " def __init__(self, name):\n", " print(\"I'm in __init__!\")\n", " self.name = name\n", " \n", " def say_my_name(self):\n", " print(self.name)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I'm in __init__!\n" ] } ], "source": [ "s = MyClass2('Carl')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Carl\n" ] } ], "source": [ "s.say_my_name()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Objects must be \"like\" things" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "class Person:\n", " def __init__(self, name):\n", " self.name = name\n", "\n", " def display_name(self):\n", " print(self.name)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "carl = Person(\"Carl Sandrock\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Carl Sandrock\n" ] } ], "source": [ "carl.display_name()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }