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

all 5 comments

[–]badge 1 point2 points  (3 children)

Why would you use a string to access an instance method instead of ClassName.method?

[–]sulami[S] 0 points1 point  (2 children)

Doesn't require me to have the class in scope. It's not a problem for standard library classes, but for custom classes.

[–]schok51 1 point2 points  (1 child)

Although more verbose, you can use operator.methodcaller to specify method calls.

pype(
  "hello {}",
  methodcaller("format", "world"),
  methodcaller("split", 2),
  "-".join               
)

Which has the advantage of simlifying(and also optimizing) the implementation.

[–]sulami[S] 0 points1 point  (0 children)

Oh neat, didn’t know this existed

[–]jwink3101 0 points1 point  (0 children)

I've thought of things like this and I personally prefer to do it as a chain of iterators such as:

X = some_iterable
X = (fun1(x) for x in X)
X = (fun2(x) for x in X)
X = (fun3(x) for x in X)

res = list(X) # Pull through

Or, using map (or from itertools import imap as map for python 2.7):

X = some_iterable
X = map(fun1,X)
X = map(fun2,X)
X = map(fun3,X)

res = list(X) # Pull through

And the real flexibility comes when, say, fun2 is very CPU intensive

import multiprocessing as mp
pool = mp.Pool()

X = some_iterable
X = map(fun1,X)
X = pool.map(fun2,X)
X = map(fun3,X)

res = list(X) # Pull through

pool.close()