{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"The
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# EPAT Session 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Executive Program in Algorithmic Trading**\n", "\n", "**_Basics of Object Orientation_**\n", "\n", "Dr. Yves J. Hilpisch | The Python Quants GmbH | http://tpq.io\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class HumanBeing(object): # <1>\n", " def __init__(self, first_name, eye_color): # <2>\n", " self.first_name = first_name # <3>\n", " self.eye_color = eye_color # <4>\n", " self.position = 0 # <5>\n", " def walk_steps(self, steps): # <6>\n", " self.position += steps # <7>" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Sandra = HumanBeing('Sandra', 'blue') # <1>" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sandra'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.first_name # <2>" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position # <2>" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Sandra.walk_steps(5) # <3>" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position # <4>" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Sandra.walk_steps(-2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A Brief Look at Standard Objects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### int" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "n = 5 # <1>" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(n) # <2>" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.numerator # <3>" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.bit_length() # <4>" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n + n # <5>" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * n # <6>" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.__sizeof__() # <7>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### list" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l = [1, 2, 3, 4] # <1>" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l) # <2>" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0] # <3>" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l.append(10) # <4>" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10, 1, 2, 3, 4, 10]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l + l # <5>" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10, 1, 2, 3, 4, 10]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * l # <6>" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(l) # <7>" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "104" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.__sizeof__() # <8>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ndarray" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np # <1>" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = np.arange(16).reshape((4, 4)) # <2>" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a # <2>" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a) # <3>" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "128" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.nbytes # <1>" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum() # <2>" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 6, 8, 10],\n", " [12, 15, 18, 21],\n", " [24, 28, 32, 36]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.cumsum(axis=0) # <3>" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22],\n", " [24, 26, 28, 30]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + a # <4>" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22],\n", " [24, 26, 28, 30]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * a # <5>" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([24, 28, 32, 36])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(a) # <6>" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(a) # <7>" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "112" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.__sizeof__() # <8>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DataFrame" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd # <1>" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.DataFrame(a, columns=list('abcd')) # <2>" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(df) # <3>" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['a', 'b', 'c', 'd'], dtype='object')" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns # <1>" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 24\n", "b 28\n", "c 32\n", "d 36\n", "dtype: int64" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.sum() # <2>" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abcd
00123
146810
212151821
324283236
\n", "
" ], "text/plain": [ " a b c d\n", "0 0 1 2 3\n", "1 4 6 8 10\n", "2 12 15 18 21\n", "3 24 28 32 36" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.cumsum() # <3>" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abcd
00246
18101214
216182022
324262830
\n", "
" ], "text/plain": [ " a b c d\n", "0 0 2 4 6\n", "1 8 10 12 14\n", "2 16 18 20 22\n", "3 24 26 28 30" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df + df # <4>" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abcd
00246
18101214
216182022
324262830
\n", "
" ], "text/plain": [ " a b c d\n", "0 0 2 4 6\n", "1 8 10 12 14\n", "2 16 18 20 22\n", "3 24 26 28 30" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * df # <5>" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 24\n", "b 28\n", "c 32\n", "d 36\n", "dtype: int64" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(df) # <6>" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "208" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.__sizeof__() # <7>" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Basics of Python Classes" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(object): # <1>\n", " pass # <2>" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi = FinancialInstrument() # <3>" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.FinancialInstrument" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(fi) # <4>" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.FinancialInstrument at 0x10f894ba8>" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi # <4>" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'<__main__.FinancialInstrument object at 0x10f894ba8>'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.__str__() # <5>" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi.price = 100 # <6>" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.price # <6>" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(object):\n", " author = 'Yves Hilpisch' # <1>\n", " def __init__(self, symbol, price): # <2>\n", " self.symbol = symbol # <3>\n", " self.price = price # <3>" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yves Hilpisch'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FinancialInstrument.author # <1>" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": true }, "outputs": [], "source": [ "aapl = FinancialInstrument('AAPL', 100) # <4>" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'AAPL'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.symbol # <5>" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yves Hilpisch'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.author # <6>" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": true }, "outputs": [], "source": [ "aapl.price = 105 # <7>" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.price # <7>" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(FinancialInstrument): # <1>\n", " def get_price(self): # <2>\n", " return self.price # <2>\n", " def set_price(self, price): # <3>\n", " self.price = price # <4>" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi = FinancialInstrument('AAPL', 100) # <5>" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi.set_price(105) # <7>" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.price # <8>" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(object):\n", " def __init__(self, symbol, price):\n", " self.symbol = symbol \n", " self.__price = price # <1>\n", " def get_price(self):\n", " return self.__price\n", " def set_price(self, price):\n", " self.__price = price" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi = FinancialInstrument('AAPL', 100)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <2>" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'FinancialInstrument' object has no attribute '__price'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__price\u001b[0m \u001b[0;31m# <3>\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'FinancialInstrument' object has no attribute '__price'" ] } ], "source": [ "fi.__price # <3>" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi._FinancialInstrument__price # <4>" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi._FinancialInstrument__price = 105 # <4>" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi.set_price(100) # <5>" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class PortfolioPosition(object):\n", " def __init__(self, financial_instrument, position_size):\n", " self.position = financial_instrument # <1>\n", " self.__position_size = position_size # <2>\n", " def get_position_size(self):\n", " return self.__position_size\n", " def update_position_size(self, position_size):\n", " self.__position_size = position_size\n", " def get_position_value(self):\n", " return self.__position_size * \\\n", " self.position.get_price() # <3>" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pp = PortfolioPosition(fi, 10)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_size()" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_value() # <3>" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.position.get_price() # <4>" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pp.position.set_price(105) # <5>" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1050" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_value() # <6>" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python Data Model" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(object):\n", " def __init__(self, x=0, y=0, z=0): # <1>\n", " self.x = x # <1>\n", " self.y = y # <1>\n", " self.z = z # <1>" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3) # <2>" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "<__main__.Vector at 0x10f8d3e80>" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __repr__(self):\n", " return 'Vector(%r, %r, %r)' % (self.x, self.y, self.z)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(1, 2, 3)" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <1>" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vector(1, 2, 3)\n" ] } ], "source": [ "print(v) # <1>" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-2)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(False)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(True)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(10)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(-1)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __abs__(self):\n", " return (self.x ** 2 + self.y ** 2 +\n", " self.z ** 2) ** 0.5 # <1>\n", " \n", " def __bool__(self):\n", " return bool(abs(self))" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, -1) # <2>" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.449489742783178" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector() # <3>" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Vector(0, 0, 0)" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'Vector' and 'Vector'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mv\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Vector' and 'Vector'" ] } ], "source": [ "v + v" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for *: 'int' and 'Vector'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'int' and 'Vector'" ] } ], "source": [ "2 * v" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __add__(self, other):\n", " x = self.x + other.x\n", " y = self.y + other.y\n", " z = self.z + other.z\n", " return Vector(x, y, z) # <1>\n", " \n", " def __mul__(self, scalar):\n", " return Vector(self.x * scalar,\n", " self.y * scalar,\n", " self.z * scalar) # <1>" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(3, 5, 7)" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v + Vector(2, 3, 4)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(2, 4, 6)" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v * 2" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "object of type 'Vector' has no len()", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: object of type 'Vector' has no len()" ] } ], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __len__(self):\n", " return 3 # <1>\n", " \n", " def __getitem__(self, i):\n", " if i in [0, -3]: return self.x\n", " elif i in [1, -2]: return self.y\n", " elif i in [2, -1]: return self.z\n", " else: raise IndexError('Index out of range.')" ] }, { "cell_type": "code", "execution_count": 109, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[0]" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[-2]" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "IndexError", "evalue": "Index out of range.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, i)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Index out of range.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: Index out of range." ] } ], "source": [ "v[3]" ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __iter__(self):\n", " for i in range(len(self)):\n", " yield self[i]" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in range(3): # <1>\n", " print(v[i]) # <1>" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for coordinate in v: # <2>\n", " print(coordinate) # <2>" ] }, { "cell_type": "code", "execution_count": 118, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(object):\n", " def __init__(self, x=0, y=0, z=0):\n", " self.x = x\n", " self.y = y\n", " self.z = z\n", " \n", " def __repr__(self):\n", " return 'Vector(%r, %r, %r)' % (self.x, self.y, self.z)\n", " \n", " def __abs__(self):\n", " return (self.x ** 2 + self.y ** 2 + self.z ** 2) ** 0.5\n", " \n", " def __bool__(self):\n", " return bool(abs(self))\n", " \n", " def __add__(self, other):\n", " x = self.x + other.x\n", " y = self.y + other.y\n", " z = self.z + other.z\n", " return Vector(x, y, z)\n", " \n", " def __mul__(self, scalar):\n", " return Vector(self.x * scalar,\n", " self.y * scalar,\n", " self.z * scalar)\n", " \n", " def __len__(self):\n", " return 3\n", " \n", " def __getitem__(self, i):\n", " if i in [0, -3]: return self.x\n", " elif i in [1, -2]: return self.y\n", " elif i in [2, -1]: return self.z\n", " else: raise IndexError('Index out of range.')\n", " \n", " def __iter__(self):\n", " for i in range(len(self)):\n", " yield self[i]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"The
" ] } ], "metadata": { "anaconda-cloud": {}, "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": 1 }