8

I usually comment my functions using multi-line docstrings with """, as mentioned in : https://www.python.org/dev/peps/pep-0257/

def func1(x):
  """
  This function does ...
  """
  ...

But what is the best way to comment a lambda function ? I hesitate between :

# This function does ...
func2 = lambda x: ...

or :

func2 = lambda x: ...
""" This function does ... """

or else ?

Eric H.
  • 2,152
  • 4
  • 22
  • 34
  • 21
    If it's important enough for a docstring, it's important enough for `def`. – Ignacio Vazquez-Abrams May 16 '18 at 12:26
  • 1
    isn't the whole point of lambda functions to have something quick and disposable? put a comment after it with a `#` maybe? – Mohammad Athar May 16 '18 at 12:27
  • 2
    I always comments prefer before the line. Its neat and clean. – SelvaS May 16 '18 at 12:28
  • 3
    Lambdas are not really intended to act as interfaces so the user should not even be exposed to them. As Ignacio said, if it warrants a docstring, it warrants a proper definition. – zwer May 16 '18 at 12:29
  • 2
    @SelvaTS Sure, although docstrings aren't comments (which get compiled away to nothing), they're strings that get attached as attributes to the object they're documenting. – PM 2Ring May 16 '18 at 12:45

1 Answers1

17

tbh, even assigning a lambda to a variable seems unpythonic to me. if it needs a name, define it as a regular function. The difference between a lambda function and a regular function is that the latter has a __name__ attribute and an explicit return statement.

if you must add a docstring to a lambda, do it like this:

f = lambda x: x + 1
f.__doc__ = """adds 1 to input-arg"""

help(f) 
# outputs the following:
help(f)
Help on function <lambda> in module __main__:

<lambda> lambda x
    adds 1 to arg

This way, the documentation is actually available to the interpreter as a function docstring.

Quoting directly from pep-8

Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

Yes:

def f(x): return 2*x

No:

f = lambda x: 2*x
Community
  • 1
  • 1
Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
  • 2
    [PEP-8](https://www.python.org/dev/peps/pep-0008/#programming-recommendations) agrees with you: "Always use a `def` statement instead of an assignment statement that binds a lambda expression directly to an identifier. [...] The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression)" – PM 2Ring May 16 '18 at 12:42
  • @PM2Ring I agree lambdas should be anonymous but perhaps you might want to annotate a key function like `sorted(lst, key = lambda x: x ** 3 - x)`, although I guess you wouln't be able to access any docstring anyway... – Chris_Rands May 16 '18 at 12:46
  • @Chris_Rands And since you can't access it (without doing crazy stuff), you might as well just annotate it with a plain comment. – PM 2Ring May 16 '18 at 12:49
  • 1
    @Chris_Rands, i feel the better approach in this instance would be to comment above the line describing why the cubic equation is the appropriate key, but the lambda itself is readable & unambiguous as is. – Haleemur Ali May 16 '18 at 12:50
  • @Chris_Rands I _occasionally_ bend the rule about binding a lambda to a name, when using a key function to `sort` and then reusing it on the following line with `itertools.groupby`, but I'm trying to break that silly habit. :) As the PEP-8 example shows, if you can write it as a lambda, you can also write it as a one line `def`. – PM 2Ring May 16 '18 at 12:52