Alternative to multi-line lambdas: Assign-anywhere def statements

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jan 25 05:27:20 CET 2015


Yawar Amin wrote:

> Hi Ethan,
> 
> On Saturday, January 24, 2015 at 9:00:12 PM UTC-5, Ethan Furman wrote:
>> [...]
>> __name__ being one of them.  One of the reasons lambda
>> is not encouraged is because its name is always '<lambda>', which just
>> ain't helpful when the smelly becomes air borne!  ;)
> 
> Doesn't the traceback tell us exactly where the lambda was called from?

Yes (assuming the source code is available, which it may not be), but even
so, which is easier?

"Oh, the exception occurred in myfunc"

versus

"Hmmm, the exception occurred in a lambda, lets see now, that was called
by "f", but f might have called seven different lambdas, which one was it?
Oh, I see, f was called by g, and when we call g, this variable is set to
foo which only happens when bar was called first, which means that the
lambda must have been this one here..."


Using lambda is trading off convenience when writing the code for ease of
debugging the code when a problem occurs. Whether that trade-off is
worthwhile or not depends on factors such as how likely the lambda is to
have a bug, how many of them there are, and whether or not there is
uncertainty as to which one is called from where.



-- 
Steven




More information about the Python-list mailing list