3 ms·
Python: one case where I believe explicit is not better than implicit is the explicit passing around of self. It just seems to add extra clutter to the code, as
by msluyter 13y ago
Python: one case where I believe explicit is not better than implicit is the explicit passing around of self. It just seems to add extra clutter to the code, as well as demanding a certain cognitive overhead in remembering that a function is defined with N parameters but is invoked with N-1.
Perhaps there are good reasons for it, but generally I prefer java's implicit 'this.'
- rprospero 13y agoI'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.