Created
March 7, 2014 18:24
-
-
Save migonzalvar/9416931 to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"name": "mixin_super" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "This snippet shows how to implement mixins and assure all methods in the final class chain are invoked.\n\n\nReference: http://rhettinger.wordpress.com/2011/05/26/super-considered-super/" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "class MixinBase(object):\n # Every mixin must inherint from this class to assure chain is not broken. It also\n # allows to invoke super() in the mixin knowing it's going to find the method in the \n # parent.\n def setUp(self):\n assert not hasattr(super(MixinBase, self), 'setUp')\n \nclass MixinOne(MixinBase):\n def setUp(self):\n print('M1')\n # Don't forget to invoke super()\n super(MixinOne, self).setUp()\n \nclass MixinTwo(MixinBase):\n def setUp(self):\n print('M2')\n # Don't forget to invoke super()\n super(MixinTwo, self).setUp()\n \nclass C12(MixinOne, MixinTwo):\n # Mixins invoked from left to right\n pass\n\nclass C21(MixinTwo, MixinOne):\n # Mixins invoked from left to right\n pass\n", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "i12 = C12()\ni12.setUp()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "M1\nM2\n" | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "i21 = C21()\ni21.setUp()", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "M2\nM1\n" | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment