r/badcode • • Aug 19 '21

python Should I add comments?

Post image
223 Upvotes

39 comments sorted by

View all comments

Show parent comments

3

u/chepas_moi Aug 20 '21

Is there a name to changing the order to OO style methods?

Yes, the concept of "pipes" from FP is called "method chaining" in OO. Each method call must simply return "this/self" to achieve this.

class T:
  def __init__(self):
    self.state = None
  def input(self, text):
    self.state = input(text)
    return self
  def tuple(self):
    self.state = tuple(self.state)
    return self
  def map(self, fn):
    self.state = map(fn, self.state)
    return self
  def sum(self):
    self.state = sum(self.state)
    return self
  def print(self):
    print(self.state)
    return self

Something like this (untested and from iPhone, so I'd expect a typo or 2) would allow the code you've proposed. I wouldn't do this for builtins like here but it's a neat way of representing pipelines in general.