Re:The unknown features of Python's `operator` module
by kevin_thibedeau on Sunday August 08, 2021 @02:58PM
(#28109192)
Lambdas don't know their name and weaken debug info.
Re:The unknown features of Python's `operator` module
by elteto on Sunday August 08, 2021 @04:24PM
(#28109785)
It seems like an implementation issue of the interpreter no? There’s no reason lambdas _have_ to be this way.
Re:The unknown features of Python's `operator` module
by jonathrg on Sunday August 08, 2021 @05:24PM
(#28110241)
lambdas don't have names, how could the interpreter possibly improve in this regard?
Re:The unknown features of Python's `operator` module
by zodiakzz on Monday August 09, 2021 @12:05AM
(#28112652)
JS arrow functions don't have names either, it's inferred from the left hand side variable name. Can't Python do something similar?
Re:The unknown features of Python's `operator` module
by jonathrg on Monday August 09, 2021 @02:29AM
(#28113310)
If you assign a lambda to a variable, you might as well use a def. A more typical use of a lambda is as an arguments to a function call. What would be the name of, say, the first argument to map(lambda x: x*2, numbers)?
Re:The unknown features of Python's `operator` module
by ViViDboarder on Sunday August 08, 2021 @12:28PM
(#28107898)
But if you’re doing that, why not just import the identical function?
Re:The unknown features of Python's `operator` module
by sharikous on Sunday August 08, 2021 @12:41PM
(#28108017)
One major point of OP was that the functions defined in operators are faster. Grandparent comment argued that there is a measurement error that invalidates this point, since properly defined lambdas (or defs) are just as fast.
Of course the point regarding the simplicity of importing a function instead of defining it still stands.
I personally wouldn't mind defining those functions as they are very immediate. So from the article I would take only the part about the more exotic "operator".
Re:The unknown features of Python's `operator` module
by klyrs on Sunday August 08, 2021 @07:00PM
(#28110986)
The timings are certainly closer, but the built-in function imported from operator is still faster than the user-defined function. Not a big deal if you're doing simple stuff, but if this code is in a hot loop that 30% can make a measurable difference.
def add_fun(x,y): return x+y
But I wouldn't mind irritating the linter with an assigned lambda. I fail to see what's the problem with it