module attributes and docstrings

Chris Angelico rosuav at gmail.com
Thu Mar 26 06:27:45 EDT 2015


On Thu, Mar 26, 2015 at 8:53 PM, Mario Figueiredo <marfig at gmail.com> wrote:
> However, lambda functions do read well in my mind and I find it hard
> to spot where they obscure the code more than a function. So the
> explicit vs. implicit part of the argument doesn't translate well with
> me. I however agree that a function declaration brings other benefits,
> like the ability to decorate or document.

A function is a function is a function, so really, it's just a
question of whether you create them with a statement (def) or an
expression (lambda). Two basic rules of thumb:

1) If you're assigning a lambda function to a simple name, then you
don't need it to be an expression, so use def.
2) If you're warping your function body to make it an expression, use def.

Basically, look at the outside and look at the inside. In some cases,
it's obvious that it's all expressions:

# Sort a list of widget objects by name
widgets.sort(key=lambda w: w.name)

Other times, it's pretty obvious that you should be using statements:

delete_name_if_blank = lambda w: delattr(w, "name") if w.name == "" else None
list(map(delete_name_if_blank, widgets))

In between, there's a lot of room to call it either way.

ChrisA



More information about the Python-list mailing list