Created
February 16, 2018 06:07
-
-
Save a-y-khan/8da41ac7eee33c370288d996bb054baa to your computer and use it in GitHub Desktop.
Python type alias example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from math import sqrt\n", | |
"from typing import List, Generic, TypeVar\n", | |
"\n", | |
"T = TypeVar('T', int, float)\n", | |
"\n", | |
"class Point(Generic[T]):\n", | |
" def __init__(self, x: T, y: T) -> None:\n", | |
" self.px = x # type: T\n", | |
" self.py = y # type: T\n", | |
" \n", | |
" @property\n", | |
" def x(self) -> T:\n", | |
" return self.px\n", | |
" \n", | |
" @property\n", | |
" def y(self) -> T:\n", | |
" return self.py\n", | |
" \n", | |
" def distance_point_coordinates(self, px: T, py: T) -> float:\n", | |
" x_diff = self.px - px\n", | |
" y_diff = self.py - py\n", | |
" return sqrt( x_diff*x_diff + y_diff*y_diff )\n", | |
"\n", | |
" def distance(self, point: 'Point') -> float:\n", | |
" x_diff = self.px - point.x\n", | |
" y_diff = self.py - point.y\n", | |
" return sqrt( x_diff*x_diff + y_diff*y_diff )\n", | |
"\n", | |
" def __str__(self) -> str:\n", | |
" return '({}, {})'.format(self.px, self.py)\n", | |
"\n", | |
" def __repr__(self) -> str:\n", | |
" return 'Point({}, {})'.format(self.px, self.py)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(1, 1)\n", | |
"(4, 2)\n", | |
"3.1622776601683795\n", | |
"3.1622776601683795\n", | |
"[Point(1, 1), Point(4, 2)]\n" | |
] | |
} | |
], | |
"source": [ | |
"p1 = Point(1, 1)\n", | |
"p2 = Point(4, 2)\n", | |
"\n", | |
"print(p1)\n", | |
"print(p2)\n", | |
"print(p1.distance_point_coordinates(p2.x, p2.y))\n", | |
"print(p2.distance(p1))\n", | |
"\n", | |
"polyline = [] # type: List[Point]\n", | |
"polyline.append(p1)\n", | |
"polyline.append(p2)\n", | |
"print(polyline)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[Point(1, 1), Point(4, 2), 'point']\n" | |
] | |
} | |
], | |
"source": [ | |
"polyline.append(\"point\")\n", | |
"print(polyline)" | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment