2

I've been attempting to learn python lately, and I decided I'd like a string representation of a lambda. I tried doing what seemed obvious at the time:

quux = lambda x: 1-abs(x)
print(quux) #<function <lambda> at 0xfacefedaf>

This was almost as helpful as JavaScript's function foo(){ <native code> }.

What I was really hoping for was something that showed what the function actually did, such as:

<function <lambda>: x:1-abs(x)>

or simply the original

x:1-abs(x)

Python doesn't seem to have any uneval function, and I'm not really sure where to look through the various lifesaving modules for something like it.

Other stuff I tried:

print(repr(quux))
print(str(quux))
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Ben Rivers
  • 129
  • 10
  • 1
    As an aside `quux = lambda x: 1-abs(x)` is bad form. You shouldn't use `lambda` for named functions. The **only** good use-case for `lambda` is for anonymous functions. – juanpa.arrivillaga May 03 '18 at 20:14
  • Ugh, my love of JavaScript's arrow functions comes to bite me in the back. Thanks for the advice, I use way too many bad practices. – Ben Rivers May 03 '18 at 21:58

0 Answers0