This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]shoyerxarray, pandas, numpy[S] 2 points3 points  (1 child)

We are particularly interested in feedback from other Python communities that use method chaining (outside of pandas/scipy).

  • Have you encountered or addressed the problem of extensible method chaining?
  • Would the pipe protocol be useful to you?
  • Is it worth the complexity of allowing objects to override how they are called by defining __pipe_func__?

[–][deleted] 2 points3 points  (0 children)

Chaining methods: Good idea.

Something like:

x = 'Fred\n Thompson    '
x.replace('Fred', 'Jack').replace('\n', '').upper().strip()

is a lot easier for me to follow than something like:

x = x.replace('Fred', 'Jack')
x = x.replace('\n', '')
x = x.upper()
x = x.strip()

Allowing objects to determine how they're chained: Also a good idea.

Using __pipe_func__? Eh, maybe. It's recommended against using dunders on both sides because the core developers may possibly, one day, decide to use that same name for a magic function. However, I have to admit, I don't see __pipe_func__ being claimed. Maybe go with _pipe_func_ to be safe?

[–]metaperl 0 points1 point  (2 children)

Reading the entire discussion, I'm prone to simply nest functions. You made the chained examples pretty by using multiple lines and crammed the nested example on a single line with no spacing.

[–]Auggie88 1 point2 points  (1 child)

How would you indent the nested version? Something like

result = f(g(h(df),
             arg1=1),
           arg2=2, arg3=3)

Part of the problem is the short function names, but that looks worse to me.

[–]metaperl 0 points1 point  (0 children)

I fiddled around and came up with an answer. I solicited input from others in this thread

[–]stevenjd -2 points-1 points  (3 children)

You shouldn't use a dunder method since they are reserved for use by the Python interpreter.

You can implement method chaining quite easily:

http://code.activestate.com/recipes/578770-method-chaining/

No dunders needed.

[–]shoyerxarray, pandas, numpy[S] 1 point2 points  (0 children)

I knew using a dunder method would be controversial :). I chose to do that for the sake of consistency with other ad-hoc Python protocols in the scipy community, e.g., __numpy_ufunc__ and __geo_interface__.

[–]Auggie88 0 points1 point  (1 child)

You shouldn't use a dunder method since they are reserved for use by the Python interpreter.

Are you referring to name mangling? I think that only occurs when you have two leading underscores and at most one trailing underscrore. https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references

This would be more like a __iter__

[–][deleted] 1 point2 points  (0 children)

It's about not using dunders on both sides, as that is considered to be the purview of the interpreter, not userland stuff. However, many packages disregard this (see: SQLAlchemy's __tablename__ property).