Last active
January 8, 2020 03:44
-
-
Save jakevdp/35ca1023805fca5b78bcd59681634beb to your computer and use it in GitHub Desktop.
Hack to make Python look like C++
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Making Python Look Like C++\n", | |
"\n", | |
"This should never be used." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"default = object()\n", | |
"\n", | |
"class Cout(object):\n", | |
" def __init__(self, s=''):\n", | |
" self.s = str(s)\n", | |
" def __lshift__(self, s):\n", | |
" return Cout(self.s + str(s))\n", | |
" def __repr__(self):\n", | |
" print(self.s, end='')\n", | |
" return ''\n", | |
"cout = Cout()\n", | |
"endl = '\\n'\n", | |
" \n", | |
"class Case(object):\n", | |
" def __call__(self, x):\n", | |
" return x\n", | |
"case = Case()\n", | |
"\n", | |
"class Switch(object):\n", | |
" def __call__(self, obj):\n", | |
" self.obj = obj\n", | |
" return self\n", | |
" def __or__(self, items):\n", | |
" output = items.get(self.obj, items[default])\n", | |
" if isinstance(output, Cout):\n", | |
" print(output.s)\n", | |
" else:\n", | |
" return output\n", | |
"switch = Switch()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Python 3.5.1\n" | |
] | |
} | |
], | |
"source": [ | |
"import sys; print(\"Python\", sys.version[:5])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello world\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cout << \"hello world\" << endl" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"about sqrt(2), as much as it matters\n" | |
] | |
} | |
], | |
"source": [ | |
"x = 1\n", | |
"\n", | |
"switch (x) | {\n", | |
" case (1):\n", | |
" cout << \"about sqrt(2), as much as it matters\",\n", | |
" case (2):\n", | |
" cout << \"about e, more or less\",\n", | |
" case (3):\n", | |
" cout << \"about pi, to a good approximation\",\n", | |
" default:\n", | |
" cout << \"I don't know what you're talking about\",\n", | |
"}" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.5", | |
"language": "", | |
"name": "python3.5" | |
}, | |
"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.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Thanks @JanSchultz! I ended up changing it to a slightly different approach
This begs for StreamIO/BytesIO.
you should make it assert dominance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is fun :-)
Shorter version, by reusing the dict instead of using lists: