3 ms·
> Just make a log() function that takes a target (file, socket, etc) and a message. Unless your logger needs to be so flexible as to work in 3rd party apps, wh
by sevensor 4y ago
> Just make a log() function that takes a target (file, socket, etc) and a message. Unless your logger needs to be so flexible as to work in 3rd party apps, why can't it be just that simple?
Of all the ways this could have been done, it seems like they chose the most complicated.
Python's lambdas get a lot of grief, but
def my_logger():
log1 = lambda msg: log(sys.stderr, msg)
log2 = lambda msg: log1(filter_function(msg))
return lambda msg: log2(other_filter_function(msg))
seems like an entirely sane and uncomplicated way build up a logging function that does what you want it to. Sure, it's not going to be high-performing code (these are Python lambdas after all), but it's not like the article's version is going to take home any prizes in that department either.
- formerly_proven 4y agoFunctions and lambdas are semantically identical, you don't need to use "lambda" to get a closure in Python.