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


Groups > comp.lang.python > #58926

Re: my favorite line of py code so far

From Peter Otten <__peter__@web.de>
Subject Re: my favorite line of py code so far
Date 2013-11-09 12:49 +0100
Organization None
References <fbbabb45-f0e3-4e7f-968f-9822607c450a@googlegroups.com> <729e093a-6312-44dc-8ed4-1dd5ea344066@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2288.1383997830.18130.python-list@python.org> (permalink)

Show all headers | View raw


Peter Cacioppi wrote:

> Peter Otten said:
> 
> 
> ">>> _ = lambda c: lambda x: c(*x)
>>>> list(map(_(P), zip([1,2,3], [6, 5, 4])))
> [Point(x=1, y=6), Point(x=2, y=5), Point(x=3, y=4)]
> 
> ? While the obvious approach would be
> 
>>>> [P(*args) for args in zip([1,2,3], [6, 5, 4])]
> [Point(x=1, y=6), Point(x=2, y=5), Point(x=3, y=4)] "
> 
> I would have coded
>>>> map(_(P), zip([1,2,3], [6, 5, 4]))
> 
> Which is very concise and (to me) quite clear. Forgive me for having a
> bias for fewer characters.

Consider developing a bias for self-explanatory names ;)

> Are you saying it's always preferable to avoid map? 

No. I'm saying that if you have a problem with multiple solutions you should 
pick the boringly obvious one if you plan to reuse or publish the code.

There is no obvious meaning attached to _ -- so don't use it.

> Is map going to be deprecated?

Not to my knowledge. I use it when I already have a function ready 
to be called with the items in a sequence. Continuing the Point example that 
could be

map(Point, xvals, yvals)

or

map(Point.from_tuple, xypairs)

> I sometimes use map, sometimes comprehensions. I suspect other people do
> the same, that's why the language supports map and comprehensions.
> 
> "there is also itertools.starmap(): "
> 
> Thanks, that's a bit closer to what I am doing.  To me the pure combinator
> is more appealing than starmap, but the presence of starmap explains why
> the library wouldn't need the combinator.

I found only a single use of starmap on my machine (and one more in the 
stdlib)

# in redis.client
all_cmds = ''.join(starmap(connection.pack_command,
                           [args for args, options in commands]))

and frankly, I would prefer

all_cmds = "".join(
    connection.pack_command(*args) for args, options in commands)

which by some strange coincidence also requires fewer letters to type.

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


Thread

my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-08 14:22 -0800
  Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-09 09:32 +1100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-08 14:43 -0800
  Re: my favorite line of py code so far Peter Otten <__peter__@web.de> - 2013-11-09 08:12 +0100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-09 01:23 -0800
    Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-09 20:28 +1100
    Re: my favorite line of py code so far Paul Rubin <no.email@nospam.invalid> - 2013-11-09 01:49 -0800
    Re: my favorite line of py code so far Peter Otten <__peter__@web.de> - 2013-11-09 12:49 +0100
    Re: my favorite line of py code so far Stefan Behnel <stefan_ml@behnel.de> - 2013-11-09 13:41 +0100
    Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-09 23:45 +1100
    Re: my favorite line of py code so far Peter Otten <__peter__@web.de> - 2013-11-09 14:49 +0100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-10 00:57 -0800
    Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-10 21:23 +1100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-10 00:59 -0800
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-10 16:00 -0800

csiph-web