4 ms·
I'm not sure that it's a good reason, but the explicit self allows for weird intermingling of functions and members. For instance, class foo: def
by rprospero 13y ago
I'm not sure that it's a good reason, but the explicit self allows for weird intermingling of functions and members. For instance,
class foo:
def __init__(self,val):
self.x=val
def square(self):
return self.x**2
#imperative way
imperative = [foo(x).square() for x in range(5)]
#functional way
s = foo.square
composed = lambda x: s(foo(x))
functional = map(composed,range(5))
print imperative==functional
The explicit self allows you to treat member functions as generic function and compose them in a functional way. Now, most functional programming isn't necessarily Pythonic. However, given a proper compose function, I could imagine it being useful to mix a long chain of function and member calls without needing to make a distinction between the two.