{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# OOP in Python\n", "Gonzalo Rios - grios@dim.uchile.cl\n", "\n", "As a object-oriented programming language" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:47:39.686817Z", "start_time": "2018-10-04T19:47:39.684348Z" } }, "outputs": [], "source": [ "p = print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Classes, types and functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:48:43.807393Z", "start_time": "2018-10-04T19:48:43.801761Z" } }, "outputs": [], "source": [ "class Test:\n", " def test():\n", " p('testing...')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:48:47.972349Z", "start_time": "2018-10-04T19:48:47.961691Z" } }, "outputs": [], "source": [ "Test" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:49:10.198576Z", "start_time": "2018-10-04T19:49:10.194837Z" } }, "outputs": [], "source": [ "type(Test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:49:34.477122Z", "start_time": "2018-10-04T19:49:34.468908Z" } }, "outputs": [], "source": [ "Test.test" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:49:43.230068Z", "start_time": "2018-10-04T19:49:43.226086Z" } }, "outputs": [], "source": [ "type(Test.test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:50:17.296143Z", "start_time": "2018-10-04T19:50:17.293227Z" } }, "outputs": [], "source": [ "Test.test()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:51:06.345650Z", "start_time": "2018-10-04T19:51:06.334043Z" } }, "outputs": [], "source": [ "t = Test()\n", "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:51:39.677211Z", "start_time": "2018-10-04T19:51:39.669078Z" } }, "outputs": [], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:51:46.184846Z", "start_time": "2018-10-04T19:51:46.145936Z" } }, "outputs": [], "source": [ "t.test()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:52:36.861231Z", "start_time": "2018-10-04T19:52:36.856318Z" } }, "outputs": [], "source": [ "class Test:\n", " def test(*args, **kwargs):\n", " p('testing...', args, kwargs)\n", "\n", "\n", "t = Test()\n", "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:52:45.833540Z", "start_time": "2018-10-04T19:52:45.830085Z" } }, "outputs": [], "source": [ "t.test(0, 1, x=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:53:30.426043Z", "start_time": "2018-10-04T19:53:30.415603Z" } }, "outputs": [], "source": [ "class Test:\n", " def test(self):\n", " p('testing...')\n", "\n", "\n", "t = Test()\n", "t.test()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:53:38.448880Z", "start_time": "2018-10-04T19:53:38.446419Z" } }, "outputs": [], "source": [ "t.test()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:53:41.433616Z", "start_time": "2018-10-04T19:53:41.428055Z" } }, "outputs": [], "source": [ "Test.test()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:53:52.099567Z", "start_time": "2018-10-04T19:53:52.096772Z" } }, "outputs": [], "source": [ "Test.test(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Objects and attributes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:56:50.385151Z", "start_time": "2018-10-04T19:56:50.380803Z" } }, "outputs": [], "source": [ "class Animal:\n", " def __init__(self, n='Bobby', c='green'):\n", " self.name = n\n", " self.color = c\n", "\n", " def sleep(self, s=1):\n", " p(\"zzzzz...\" * s)\n", "\n", " def talk(self):\n", " p(\"gggrrr...\")\n", "\n", " def compare(self, a):\n", " p('name:', self.name, ' vs ', a.name)\n", " p('color:', self.color, ' vs ', a.color)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:59:00.160180Z", "start_time": "2018-10-04T19:59:00.154749Z" } }, "outputs": [], "source": [ "a1 = Animal('a1', 'black')\n", "a2 = Animal('a2', 'white')\n", "\n", "_ = a1.talk(), a1.sleep(), a2.compare(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:59:18.500295Z", "start_time": "2018-10-04T19:59:18.495596Z" } }, "outputs": [], "source": [ "a1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:59:47.698000Z", "start_time": "2018-10-04T19:59:47.694963Z" } }, "outputs": [], "source": [ "setattr(a1, 'dot2', lambda x: x * 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:59:51.343277Z", "start_time": "2018-10-04T19:59:51.339589Z" } }, "outputs": [], "source": [ "a1.dot2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T19:59:54.203028Z", "start_time": "2018-10-04T19:59:54.198866Z" } }, "outputs": [], "source": [ "a1.dot2(9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:00:11.759763Z", "start_time": "2018-10-04T20:00:11.755847Z" } }, "outputs": [], "source": [ "getattr(a1, 'dot2')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:00:15.292380Z", "start_time": "2018-10-04T20:00:15.288399Z" } }, "outputs": [], "source": [ "hasattr(a1, 'color')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:00:18.506621Z", "start_time": "2018-10-04T20:00:18.504228Z" } }, "outputs": [], "source": [ "delattr(a1, 'color')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:00:19.715627Z", "start_time": "2018-10-04T20:00:19.712096Z" } }, "outputs": [], "source": [ "hasattr(a1, 'color')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:00:49.340125Z", "start_time": "2018-10-04T20:00:49.329690Z" } }, "outputs": [], "source": [ "a1.compare(a2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:01:48.221844Z", "start_time": "2018-10-04T20:01:48.217159Z" } }, "outputs": [], "source": [ "id(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:01:51.190505Z", "start_time": "2018-10-04T20:01:51.187111Z" } }, "outputs": [], "source": [ "hash(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:01:54.554245Z", "start_time": "2018-10-04T20:01:54.550637Z" } }, "outputs": [], "source": [ "repr(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:03:08.782414Z", "start_time": "2018-10-04T20:03:08.771334Z" } }, "outputs": [], "source": [ "ascii(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:03:13.860607Z", "start_time": "2018-10-04T20:03:13.852998Z" } }, "outputs": [], "source": [ "format(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:04:08.197447Z", "start_time": "2018-10-04T20:04:08.192688Z" } }, "outputs": [], "source": [ "vars(a2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:04:20.009038Z", "start_time": "2018-10-04T20:04:20.005576Z" } }, "outputs": [], "source": [ "callable(a1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:04:23.660462Z", "start_time": "2018-10-04T20:04:23.654653Z" } }, "outputs": [], "source": [ "callable(a1.dot2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inheritance" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:05:39.665612Z", "start_time": "2018-10-04T20:05:39.659435Z" } }, "outputs": [], "source": [ "class Wolf(Animal):\n", " def bark(self):\n", " p(\"Grr...\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:05:47.581920Z", "start_time": "2018-10-04T20:05:47.576472Z" } }, "outputs": [], "source": [ "bigby = Wolf(\"Big Bad Wolf\", \"black\")\n", "p(bigby.color)\n", "bigby.sleep()\n", "bigby.bark()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:08:48.290875Z", "start_time": "2018-10-04T20:08:48.287346Z" } }, "outputs": [], "source": [ "class Dog(Wolf):\n", " def bark(self):\n", " p(\"Woof!\")\n", " def growls(self):\n", " super().bark()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:09:02.729667Z", "start_time": "2018-10-04T20:09:02.726252Z" } }, "outputs": [], "source": [ "fido = Dog(\"Fido\", \"brown\")\n", "p(fido.color)\n", "fido.sleep(2)\n", "fido.bark()\n", "fido.growls()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:09:53.026740Z", "start_time": "2018-10-04T20:09:53.011011Z" } }, "outputs": [], "source": [ "super?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:42.100379Z", "start_time": "2018-10-04T20:27:42.093424Z" } }, "outputs": [], "source": [ "from functools import partialmethod\n", "\n", "class Cat(Animal):\n", " legs = 4\n", " sleep_night = partialmethod(Animal.sleep, 10)\n", " \n", " def __init__(self, n, c):\n", " super().__init__(n, c)\n", " self._nice = True\n", " \n", " def talk(self):\n", " p(\"meow...\")\n", " \n", " def compare(self, cat):\n", " super().compare(cat)\n", " p('nice:',self._nice, ' vs ', cat._nice)\n", " \n", " @classmethod\n", " def black_cat(cls, name):\n", " return cls(name, \"black\")\n", " \n", " @staticmethod\n", " def is_black(other_cat):\n", " if other_cat.color == \"black\":\n", " return other_cat.name+\" is a black cat\"\n", " else:\n", " return other_cat.name+\" isn't black cat!\"\n", " \n", " @property\n", " def is_nice(self):\n", " return self._nice\n", " @is_nice.setter\n", " def is_nice(self, value):\n", " if value == False:\n", " self._nice = value" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:43.539761Z", "start_time": "2018-10-04T20:27:43.529550Z" } }, "outputs": [], "source": [ "c1 = Cat('Grass', 'green')\n", "\n", "c1.talk(), c1.sleep_night()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:43.850607Z", "start_time": "2018-10-04T20:27:43.841632Z" } }, "outputs": [], "source": [ "c2 = Cat('Sky', 'blue')\n", "c1.compare(c2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:44.117328Z", "start_time": "2018-10-04T20:27:44.110424Z" } }, "outputs": [], "source": [ "b = Cat.black_cat('Dark')\n", "b.compare(c2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:44.437071Z", "start_time": "2018-10-04T20:27:44.430829Z" } }, "outputs": [], "source": [ "b.is_nice = False\n", "b.compare(c2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:44.782229Z", "start_time": "2018-10-04T20:27:44.776372Z" } }, "outputs": [], "source": [ "b.is_nice = True\n", "b.compare(c2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:45.423608Z", "start_time": "2018-10-04T20:27:45.415676Z" } }, "outputs": [], "source": [ "c1.color, b.color" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:45.858161Z", "start_time": "2018-10-04T20:27:45.850858Z" } }, "outputs": [], "source": [ "c1.legs, Cat.legs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:46.266254Z", "start_time": "2018-10-04T20:27:46.260877Z" } }, "outputs": [], "source": [ "Cat.legs = 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:46.894071Z", "start_time": "2018-10-04T20:27:46.885793Z" } }, "outputs": [], "source": [ "Cat.legs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:27:47.349099Z", "start_time": "2018-10-04T20:27:47.342864Z" } }, "outputs": [], "source": [ "c1.legs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:28:07.616895Z", "start_time": "2018-10-04T20:28:07.612910Z" } }, "outputs": [], "source": [ "Cat.is_black(c1), Cat.is_black(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:07.871537Z", "start_time": "2018-10-04T20:29:07.867415Z" } }, "outputs": [], "source": [ "Cat.is_black(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:08.126297Z", "start_time": "2018-10-04T20:29:08.121003Z" } }, "outputs": [], "source": [ "b.talk()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:15.227269Z", "start_time": "2018-10-04T20:29:15.223054Z" } }, "outputs": [], "source": [ "type(a1), type(b), type(Animal), type(Cat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:39.216693Z", "start_time": "2018-10-04T20:29:39.210370Z" } }, "outputs": [], "source": [ "issubclass(Cat, Animal)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:42.115855Z", "start_time": "2018-10-04T20:29:42.111908Z" } }, "outputs": [], "source": [ "issubclass(Cat, dict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:47.926729Z", "start_time": "2018-10-04T20:29:47.923296Z" } }, "outputs": [], "source": [ "isinstance(b, Cat)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:51.564944Z", "start_time": "2018-10-04T20:29:51.561243Z" } }, "outputs": [], "source": [ "isinstance(b, Animal)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:29:54.246795Z", "start_time": "2018-10-04T20:29:54.243010Z" } }, "outputs": [], "source": [ "isinstance(b, list)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:30:05.030470Z", "start_time": "2018-10-04T20:30:05.017543Z" } }, "outputs": [], "source": [ "isinstance(a1, Animal)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:30:06.823636Z", "start_time": "2018-10-04T20:30:06.819525Z" } }, "outputs": [], "source": [ "isinstance(a1, Cat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Magic methods\n", "http://minhhh.github.io/posts/a-guide-to-pythons-magic-methods" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:30:57.465539Z", "start_time": "2018-10-04T20:30:57.462161Z" } }, "outputs": [], "source": [ "help(dir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:31:11.028050Z", "start_time": "2018-10-04T20:31:11.022685Z" } }, "outputs": [], "source": [ "dir(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:32:06.851664Z", "start_time": "2018-10-04T20:32:06.847705Z" } }, "outputs": [], "source": [ "b.__class__" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:32:37.796885Z", "start_time": "2018-10-04T20:32:37.793390Z" } }, "outputs": [], "source": [ "b.__dir__" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:32:43.496165Z", "start_time": "2018-10-04T20:32:43.490427Z" } }, "outputs": [], "source": [ "sorted(b.__dir__()) == sorted(dir(b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:33:06.618724Z", "start_time": "2018-10-04T20:33:06.613130Z" } }, "outputs": [], "source": [ "b.__dict__" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:34:01.683481Z", "start_time": "2018-10-04T20:34:01.677803Z" } }, "outputs": [], "source": [ "vars(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:34:09.193134Z", "start_time": "2018-10-04T20:34:09.185235Z" } }, "outputs": [], "source": [ "dir()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:34:43.756985Z", "start_time": "2018-10-04T20:34:43.751203Z" } }, "outputs": [], "source": [ "vars()[dir()[-1]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:40:05.569646Z", "start_time": "2018-10-04T20:40:05.560510Z" } }, "outputs": [], "source": [ "class Cat2(Cat):\n", " def __str__(self):\n", " return '{} is a {} cat and it is{} nice'.format(self.name, self.color, '' if self._nice else ' not')\n", " \n", " def __repr__(self):\n", " return self.__str__()\n", " \n", " def __len__(self):\n", " return 4\n", " \n", " def __int__(self):\n", " return 8\n", "\n", " def __neg__(self):\n", " print(\"Just go home and die \" + str(self.name)) \n", "\n", " def __pos__(self):\n", " print(\"Rise up and walk \" + str(self.name)) \n", " \n", " @property\n", " def __dict__(self):\n", " return {k:v for k,v in super().__dict__.items() if k not in ['_nice']}\n", " \n", "b = Cat2('Garfield', 'orange')\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:40:39.668906Z", "start_time": "2018-10-04T20:40:39.665303Z" } }, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:40:40.813645Z", "start_time": "2018-10-04T20:40:40.809753Z" } }, "outputs": [], "source": [ "str(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:40:41.364103Z", "start_time": "2018-10-04T20:40:41.355706Z" } }, "outputs": [], "source": [ "len(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:40:41.673044Z", "start_time": "2018-10-04T20:40:41.665128Z" } }, "outputs": [], "source": [ "int(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:36:14.399102Z", "start_time": "2018-10-04T20:36:14.396308Z" } }, "outputs": [], "source": [ "-b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:36:17.322356Z", "start_time": "2018-10-04T20:36:17.319463Z" } }, "outputs": [], "source": [ "+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Magic Comparation" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:41:36.653917Z", "start_time": "2018-10-04T20:41:36.648685Z" } }, "outputs": [], "source": [ "b == b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:41:39.030195Z", "start_time": "2018-10-04T20:41:39.024707Z" } }, "outputs": [], "source": [ "b >= b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:42:06.282731Z", "start_time": "2018-10-04T20:42:06.276874Z" } }, "outputs": [], "source": [ "class Cat3(Cat2):\n", " def __lt__(self, other):\n", " p(\"__lt__: A < B\")\n", "\n", " def __le__(self, other):\n", " p(\"__le__: A <= B\")\n", "\n", " def __eq__(self, other):\n", " p(\"__eq__: A == B\")\n", "\n", " def __ne__(self, other):\n", " p(\"__ne__: A != B\")\n", "\n", " def __gt__(self, other):\n", " p(\"__gt__: A > B\")\n", "\n", " def __ge__(self, other):\n", " p(\"__ge__: A >= B\")\n", "\n", "\n", "a = Cat3('Keyboard Cat', 'orange')\n", "b = Cat3('Garfield', 'orange')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:42:11.390364Z", "start_time": "2018-10-04T20:42:11.385970Z" } }, "outputs": [], "source": [ "a < b\n", "a <= b\n", "a == b\n", "a != b\n", "a > b\n", "a >= b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:43:11.038396Z", "start_time": "2018-10-04T20:43:11.032537Z" } }, "outputs": [], "source": [ "from functools import total_ordering\n", "\n", "@total_ordering\n", "class Cat3(Cat2):\n", " \n", " def __lt__(self, other):\n", " if self._nice != other._nice:\n", " return other._nice\n", " elif len(self.name) != len(other.name):\n", " return len(self.name) < len(other.name)\n", " else:\n", " return len(self.color) < len(other.color)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:43:12.936130Z", "start_time": "2018-10-04T20:43:12.933479Z" } }, "outputs": [], "source": [ "a = Cat3('Keyboard Cat', 'orange')\n", "b = Cat3('Garfield', 'orange')\n", "c = Cat3('Garfield', 'black')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:43:14.014526Z", "start_time": "2018-10-04T20:43:14.010342Z" } }, "outputs": [], "source": [ "a < b, a <= b, a > b, a >= b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:43:14.705830Z", "start_time": "2018-10-04T20:43:14.696796Z" } }, "outputs": [], "source": [ "c < b, c <= b, c > b, c >= b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:43:18.624407Z", "start_time": "2018-10-04T20:43:18.620168Z" } }, "outputs": [], "source": [ "b.is_nice = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:43:18.980677Z", "start_time": "2018-10-04T20:43:18.974728Z" } }, "outputs": [], "source": [ "c < b, c <= b, c > b, c >= b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Magic Indexed" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:02.555314Z", "start_time": "2018-10-04T20:52:02.534855Z" } }, "outputs": [], "source": [ "class MyComplex:\n", " def __init__(self, a, b):\n", " self.a = a\n", " self.b = b\n", "\n", " def __int__(self):\n", " return self.a + self.b\n", "\n", " def __str__(self):\n", " return \"MyComplex(\" + str(self.a) + \",\" + str(self.b) + \")\"\n", "\n", " def __len__(self):\n", " return 2\n", "\n", " def __getitem__(self, index):\n", " p('get '+str(index))\n", " if index % 2 == 0:\n", " return self.a\n", " else:\n", " return self.b\n", "\n", " def __setitem__(self, index, value):\n", " p('set '+str(index))\n", " if index % 2 == 0:\n", " self.a = value\n", " else:\n", " self.b = value\n", "\n", " def __delitem__(self, index):\n", " p('del '+str(index))\n", " if index % 2 == 0:\n", " self.a = 0\n", " else:\n", " self.b = 0\n", "\n", " def __del__(self):\n", " p(\"Deleting \" + str(self))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:02.788504Z", "start_time": "2018-10-04T20:52:02.780538Z" } }, "outputs": [], "source": [ "first = MyComplex(5, 7)\n", "second = MyComplex(3, 9)\n", "\n", "p(int(first))\n", "p(len(first))\n", "p(str(first))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:03.056016Z", "start_time": "2018-10-04T20:52:03.045956Z" } }, "outputs": [], "source": [ "first[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:03.292455Z", "start_time": "2018-10-04T20:52:03.287534Z" } }, "outputs": [], "source": [ "first[0] = 6" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:03.957991Z", "start_time": "2018-10-04T20:52:03.949267Z" } }, "outputs": [], "source": [ "str(first)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:04.222208Z", "start_time": "2018-10-04T20:52:04.217689Z" } }, "outputs": [], "source": [ "del(first[1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:04.496583Z", "start_time": "2018-10-04T20:52:04.489420Z" } }, "outputs": [], "source": [ "str(first)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:05.024302Z", "start_time": "2018-10-04T20:52:05.013441Z" } }, "outputs": [], "source": [ "[second[k] for k in range(10)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:52:06.819390Z", "start_time": "2018-10-04T20:52:06.813817Z" } }, "outputs": [], "source": [ "first = second" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:51:19.338302Z", "start_time": "2018-10-04T20:51:19.332738Z" } }, "outputs": [], "source": [ "del first" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-04T20:51:20.245168Z", "start_time": "2018-10-04T20:51:20.242477Z" } }, "outputs": [], "source": [ "del second" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Magic operators\n", "https://docs.python.org/3/library/operator.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T22:43:50.265791Z", "start_time": "2017-08-16T22:43:50.171888Z" } }, "outputs": [], "source": [ "# Magic Operators\n", "class NewComplex:\n", " def __init__(self, a, b):\n", " self.a = a\n", " self.b = b\n", "\n", " def operation(self, other):\n", " return \"operation\"\n", "\n", " def __str__(self):\n", " return \"(\" + str(self.a) + \",\" + str(self.b) + \")\"\n", "\n", " def __add__(self, other):\n", " return NewComplex(self.a + other.a, self.b + other.b)\n", "\n", " def __sub__(self, other):\n", " return NewComplex(self.a - other.a, self.b - other.b)\n", "\n", " def __mul__(self, other):\n", " return NewComplex(self.a * other.a - self.b * other.b, self.a * other.b + self.b * other.a)\n", "\n", " def __truediv__(self, other):\n", " return NewComplex((self.a * other.a + self.b * other.b) / (self.b * self.b + other.b * other.b),\n", " (self.b * other.a - self.a * other.b) / (self.b * self.b + other.b * other.b))\n", "\n", " def __floordiv__(self, other):\n", " return \"A.__floordiv__(B) = A//B\"\n", "\n", " def __mod__(self, other):\n", " return \"A.__mod__(B) = A%B\"\n", "\n", " def __pow__(self, other):\n", " return \"A.__pow__(B) = A**B\"\n", "\n", " def __and__(self, other):\n", " return \"A.__and__(B) = A&B\"\n", "\n", " def __xor__(self, other):\n", " return \"A.__xor__(B) = A^B\"\n", "\n", " def __or__(self, other):\n", " return \"A.__or__(B) = A|B\"\n", "\n", " def __lt__(self, other):\n", " return \"A.__lt__(B) = AB\"\n", "\n", " def __ge__(self, other):\n", " return \"A.__ge__(B) = A>=B\"\n", "\n", " def __radd__(self, other):\n", " return \"A.__radd__(B) = B + A\"\n", "\n", " def __rsub__(self, other):\n", " return \"A.__rsub__(B) = B - A\"\n", "\n", "\n", "first = NewComplex(2 / 3, 5 / 7)\n", "second = NewComplex(1 / 3, 2 / 7)\n", "\n", "first.operation(second)\n", "\n", "p(first + second)\n", "p(first - second)\n", "p(first * second)\n", "p(first / second)\n", "p(first // second)\n", "p(first % second)\n", "p(first ** second)\n", "p(first & second)\n", "p(first ^ second)\n", "p(first | second)\n", "p(first < second)\n", "p(first <= second)\n", "p(first == second)\n", "p(first != second)\n", "p(first > second)\n", "p(first >= second)\n", "p(3 + first)\n", "p(5 - second)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Property, callable and persistence" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T19:38:03.394375Z", "start_time": "2017-08-16T19:38:03.379950Z" } }, "outputs": [], "source": [ "class C(object):\n", " _x = None\n", " def getx(self): \n", " return self._x\n", " def setx(self, value): \n", " self._x = value\n", " def delx(self): \n", " del self._x\n", " x = property(getx, setx, delx, \"I'm the 'x' property.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T19:38:16.079577Z", "start_time": "2017-08-16T19:38:16.073446Z" } }, "outputs": [], "source": [ "c = C()\n", "c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T19:38:16.489209Z", "start_time": "2017-08-16T19:38:16.487052Z" } }, "outputs": [], "source": [ "c.x = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T19:38:16.820039Z", "start_time": "2017-08-16T19:38:16.815880Z" } }, "outputs": [], "source": [ "c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T19:38:21.088452Z", "start_time": "2017-08-16T19:38:21.085666Z" } }, "outputs": [], "source": [ "del(c.x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T19:38:21.922343Z", "start_time": "2017-08-16T19:38:21.920016Z" } }, "outputs": [], "source": [ "c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T20:57:00.183175Z", "start_time": "2017-08-16T20:57:00.176782Z" } }, "outputs": [], "source": [ "#Decorators make defining new properties or modifying existing ones easy:\n", "class C(object):\n", " _x = None\n", " @property\n", " def x(self):\n", " print(\"I am the 'x' property.\")\n", " return self._x\n", " @x.setter\n", " def x(self, value):\n", " self._x = value\n", " @x.deleter\n", " def x(self):\n", " del self._x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T20:57:00.589044Z", "start_time": "2017-08-16T20:57:00.586181Z" } }, "outputs": [], "source": [ "c = C()\n", "c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T20:57:21.162222Z", "start_time": "2017-08-16T20:57:21.156251Z" } }, "outputs": [], "source": [ "c.x = 0\n", "c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T20:57:23.166478Z", "start_time": "2017-08-16T20:57:23.164279Z" } }, "outputs": [], "source": [ "del c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T20:57:23.622538Z", "start_time": "2017-08-16T20:57:23.620040Z" } }, "outputs": [], "source": [ "c.x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T22:44:45.437416Z", "start_time": "2017-08-16T22:44:45.429245Z" } }, "outputs": [], "source": [ "class MyFunction:\n", " def __init__(self, x):\n", " self.x = x\n", " def __call__(self, *args, **kwargs):\n", " return self.x*args[0]\n", " def __format__(self, *args, **kwargs):\n", " return 'f'+str(self.x)\n", " def __len__(self, *args, **kwargs):\n", " if type(self.x) is int:\n", " return self.x\n", " return len(self.x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T22:44:46.790792Z", "start_time": "2017-08-16T22:44:46.785964Z" } }, "outputs": [], "source": [ "f = MyFunction(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T22:44:50.852393Z", "start_time": "2017-08-16T22:44:50.846139Z" } }, "outputs": [], "source": [ "callable(f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2017-08-16T22:44:56.230992Z", "start_time": "2017-08-16T22:44:56.227152Z" } }, "outputs": [], "source": [ "f(2, a=\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#import copy, pickle, inspect" ] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }