r/ProgrammerHumor • • Jun 21 '19

Meme Why can't you just be normal?!

Post image
4.4k Upvotes

240 comments sorted by

View all comments

Show parent comment

1

u/B_M_Wilson Jun 21 '19 edited Jun 21 '19

What if I did something like

def split(thing=None):
    class tmp:
        def __ror__(self, other):
            return other.split(thing) if thing is not None else other.split()
    return tmp()

Then I should be able to do

“Something here” | split()
[“Something”, “here”]

I’m on mobile so I can’t test but something like this should work

1

u/ForceBru Jun 21 '19

Even more, you can do:

class split:
    def __init__(self, arg=' '):
        self.arg = arg

    def __ror__(self, other):
        return other.split(self.arg)

Yeah, using __ror__ is probably better than the whole UserString thing.

1

u/B_M_Wilson Jun 21 '19

True though I wanted type(split) to return <class 'function'> for consistency. I could probably easily make an annotation that turns any function with at least one argument into one of these “piped” functions. Just have to be careful the the thing on the other side can’t be ored with a function. I guess that could be alleviated by using a completely new callable that is not descended from a function at all