-1

Is it possible to modify how the function or lambda is printed in python (from within the console)? As an example:

>>> def F():
...     return 2
>>> F
<function F at 0x10442da28>

>>> L = lambda: 2
>>> L
<function <lambda> at 0x10442daa0>

For example, printing it like this instead:

>>> L
<lambda function "L">

>>> F
<function "F">

I am debugging some functional code so would like to be able to see the name of functions (mostly lambdas) instead of just the generic <function <lambda> whenever one is used.

David542
  • 104,438
  • 178
  • 489
  • 842
  • Does this answer your question? [How can we print the variable name along with its value in python, which will be useful during debugging?](https://stackoverflow.com/questions/31133627/how-can-we-print-the-variable-name-along-with-its-value-in-python-which-will-be) – Samwise May 16 '21 at 01:16
  • 2
    The purpose of lambda is to create an anonymous function, which means that by definition the function itself doesn't have a name. It sounds like what you want is to print out the name of the *variable* that you're using to reference that function. – Samwise May 16 '21 at 01:17
  • @Samwise yes, that's correct. – David542 May 16 '21 at 01:19
  • 2
    If you want your functions to have names, you shouldn't *use* `lambda`. There's no guarantee that a lambda will actually be assigned to a variable, and when it is, that's a case for `def`, not `lambda`. – user2357112 May 16 '21 at 01:19
  • @user2357112supportsMonica yes I agree, this is more for debugging within the console though and it makes things quite convenient, even if the point of lambda is it being anonymous. Having something like `T=lambda a: lambda b: a` and `F=lambda a: lambda b: b` and seeing how other items may evaluate (to either `T` or `F`) is almost impossible when debugging if the lambda doesn't have some name. – David542 May 16 '21 at 01:19
  • 2
    @David542: Trying to write Python like lambda calculus is itself a bad move - if you want to write lambda calculus style, a functional language would be much more appropriate. Python has very strict recursion depth limits (which `sys.setrecursionlimit` *does not* remove), and it has no support for tail call elimination. – user2357112 May 16 '21 at 01:29
  • 1
    Also note that nested lambdas like that are one of the many cases that break iota's answer. – user2357112 May 16 '21 at 01:30
  • Also, if you want to know whether a function is `T` or `F`, just do `func(True)(False)` instead of trying to find a name for it. – user2357112 May 16 '21 at 01:33
  • @user2357112supportsMonica could you explain how that would work? – David542 May 16 '21 at 01:41
  • 1
    Your `T` and `F` are curried versions of `def T(a, b): return a` and `def F(a, b): return b`. Hopefully you see why calling `T(True, False)` and `F(True, False)` with the uncurried forms would return `True` and `False` respectively. – user2357112 May 16 '21 at 01:48
  • @user2357112supportsMonica oh, got it. thanks for explaining that: `>>> T(True)(False)` `True` `>>> F(True)(False)` `False`. – David542 May 16 '21 at 01:52

1 Answers1

1

If in the (global) scope of the interpreter, a simple way to lookup the function would be using the globals() dict, something like:

def get_name(f):
    try: name = [k for k,v in globals().items() if v==f][0]
    except: return f
    return '%s: %s' % (name, f)

And using it:

N=get_name
T=lambda x: lambda y: x
F=lambda x: lambda y: y

>>> N(T(2)(3))
2
>>> N(T(T)(3))
'T: <function <lambda> at 0x10d4a8160>'
David542
  • 104,438
  • 178
  • 489
  • 842