4 ms·
First I don't see how naming a function '_' translates into calling it a lambda. Also, i18n much? Doesn't like this def handle_click(self): foo = self.
by njharman 17y ago
First I don't see how naming a function '_' translates into calling it a lambda. Also, i18n much?
Doesn't like this
def handle_click(self):
foo = self.flim()
if foo:
self.flam(foo)
onClick(handle_click)
And thinks this mess is better
with onClick():
def _(self):
foo = self.flim()
if foo:
self.flam()
My problems with 2nd include 1) it's ugly, convulted and I'd argue misusing with, 2) by no longer having a name for flimflamfunc you loose documentation, 3) hard to reuse flimflamfunc, 4) hard to decorate, mock, dynamically replace flimflamfunc.
Author's main dislike about first code block is "we're required to define the function body before the place where it will be actually used." Which I think is preferable but accept that's my opinion. They're ways to solve that which are easier, cleaner, and more pythonic than author's proposed solution.
def callback(onFoo):
def inner(func):
onFoo(func)
return func
return inner
@callback(onClick)
def handle_click(self):
pass
There might be good reasons for multi-line lambdas, but I don't see any presented here. And if we assume multi-line lambdas are awesome, this is not an acceptable implimentation.
- defunkt 17y agoTotally agree. I think, more importantly than being 'ugly' or hard to mock, your second example is hard to understand. It took me too long to realize it was identical to your first example.
- scott_s 17y agoTo be fair, I believe problems 2), 3), 4) and your preference to define the function before it's used would all apply to an in-language multiline lambda as well.
- abecedarius 17y agoAbout these problems point by point: 1) Agreed. I'd never want to see this technique in my programs. 2) What stops you from giving it a name? 3) Putting code inline is presumably a conscious decision not to extract it into a wider scope. 4) How's it hard to decorate? For mocking and dynamically replacing, again, that goes for any inline code. Objections 3 and 4 have to argue against functions appearing in non-global scopes in general.