Skip to content

Instantly share code, notes, and snippets.

@jakevdp
Last active January 8, 2020 03:44
Show Gist options
  • Save jakevdp/35ca1023805fca5b78bcd59681634beb to your computer and use it in GitHub Desktop.
Save jakevdp/35ca1023805fca5b78bcd59681634beb to your computer and use it in GitHub Desktop.
Hack to make Python look like C++
Display the source blob
Display the rendered blob
Raw
{
"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
}
@jankatins
Copy link

This is fun :-)

Shorter version, by reusing the dict instead of using lists:

default = object()

class Cout(object):
    def __lshift__(self, obj):
        return obj
cout = Cout()

def case(x):
    return x

class Switch(object):
    def __call__(self, obj):
        self.obj = obj
        return self
    def __or__(self, D):
        output = D.get(self.obj, default)
        if output == default:
            try:
                output = D[default]
            except KeyError:
                raise RuntimeError("Missing default value")
        print(output)
switch = Switch()

@jakevdp
Copy link
Author

jakevdp commented Jun 7, 2016

Thanks @JanSchultz! I ended up changing it to a slightly different approach

@d33tah
Copy link

d33tah commented Jun 13, 2017

This begs for StreamIO/BytesIO.

@dullbananas
Copy link

you should make it assert dominance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment