{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Random Live Hacking" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Variables are just names that point to objects\n", "* Variables are not typed - the type is in the objects they points to" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "i = 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "i = 1.5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "i = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`int` is also a variable - a name. It points to an object containing the type for all int objects." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(int)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is the type of that `int` type object? What's the type of type objects, generally?" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(int))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aha ..." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('42')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`int` is just a name, so let another name point to the same object (type `int`)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "franziska = int" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "franziska('42')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assignments to `int` are possible. Although rarely useful." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "int = 42" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not callable\n" ] } ], "source": [ "try:\n", " int('42')\n", "except BaseException as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remove the last name for type `int`, so it is gone forever ..." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "del franziska" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ah, it is still there: `1` is an integer literal, so it has to carry a reference to its type. This comes to our rescue: we can restore the name `int` to point to what is should." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(1)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "int = type(1)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('42')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pooh!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mutable, Immutable" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0x7f03d41165f0\n", "0x7f03d41165f0\n" ] } ], "source": [ "l1 = [1, 2, 3]\n", "l2 = l1\n", "print(hex(id(l1)))\n", "print(hex(id(l2)))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(4)\n", "l1" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exception, demonstrated using dict access" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "d = {1:'one', 2:'two'}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'two'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we access a nonexisting dictionary member, demonstrating how we react on the error that ensues.\n", "* Catch the exception by type (`KeyError`)\n", "* Use the `logging` module to format the output (stack trace etc.). See its docs for more." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ERROR:root:verdammt!\n", "Traceback (most recent call last):\n", " File \"\", line 5, in \n", " d[3]\n", "KeyError: 3\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Jessas: 3\n" ] } ], "source": [ "import sys\n", "import logging\n", "\n", "try:\n", " d[3]\n", "except KeyError as e:\n", " print('Jessas:', e)\n", " logging.exception('verdammt!')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(KeyError))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All Exceptions are derived from `BaseException` " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(KeyError, BaseException)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even the ones that you define yourself have to be derived from `BaseException`. Better yet, derive them from `Exception` which should the base for all user-defined exceptions." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cannot raise str: exceptions must derive from BaseException\n" ] } ], "source": [ "try:\n", " raise 'bummer!'\n", "except BaseException as e:\n", " print('Cannot raise str:', e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indices and Slicing" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "l = ['Peter', 'Paul', 'Mary']" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Peter'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "peter = l[0]\n", "peter" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Pet'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "peter[0:3]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Pet'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0][0:3]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Pet'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "peter[:3]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [2,3,4]\n", "l[0:0]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0:0] = [0,1]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are perfectly iterable" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "blah\n", "bloh\n", "blech\n" ] } ], "source": [ "for item in ['blah', 'bloh', 'blech']:\n", " print(item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`range` is not a list, but a generator. A list would *contain* (allocate in memory) all that it has. `range` produces the next element on demand - as the `for` loop iterates." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(5,10):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(10)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "r = range(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterator protocol" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iterator = iter(r)\n", "iterator" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(iterator)\n", "next(iterator)\n", "next(iterator)\n", "next(iterator)\n", "next(iterator)\n", "next(iterator)\n", "next(iterator)\n", "next(iterator)\n", "next(iterator)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(iterator)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "# final element already consumed, consume another one\n", "try:\n", " next(iterator)\n", "except StopIteration as e:\n", " print(e)" ] } ], "metadata": { "celltoolbar": "Slideshow", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }