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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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