Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88040

Re: module attributes and docstrings

References <fg92halkvjecqedpogsmkov3o53f0i922b@4ax.com> <mailman.110.1427225646.10327.python-list@python.org> <cgl7hadtmma78b3g2617tr0f19897gm7tu@4ax.com>
Date 2015-03-26 21:27 +1100
Subject Re: module attributes and docstrings
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.193.1427365668.10327.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Mar 26, 2015 at 8:53 PM, Mario Figueiredo <marfig@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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

module attributes and docstrings Mario Figueiredo <marfig@gmail.com> - 2015-03-24 09:55 +0100
  Re: module attributes and docstrings Chris Angelico <rosuav@gmail.com> - 2015-03-24 21:29 +1100
  Re: module attributes and docstrings Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-24 22:49 +1100
    Re: module attributes and docstrings Mario Figueiredo <marfig@gmail.com> - 2015-03-26 10:48 +0100
      Re: module attributes and docstrings Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-27 18:31 +1100
        Re: module attributes and docstrings Marko Rauhamaa <marko@pacujo.net> - 2015-03-27 09:49 +0200
  Re: module attributes and docstrings Terry Reedy <tjreedy@udel.edu> - 2015-03-24 15:33 -0400
    Re: module attributes and docstrings Mario Figueiredo <marfig@gmail.com> - 2015-03-26 10:53 +0100
      Re: module attributes and docstrings Chris Angelico <rosuav@gmail.com> - 2015-03-26 21:27 +1100

csiph-web