Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.07; 'class,': 0.07; 'subject:code': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'iterable,': 0.16; 'itertools': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'self.y': 0.16; 'starmap': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'library': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'tend': 0.24; "haven't": 0.24; 'header:X-Complaints-To:1': 0.27; 'needed.': 0.30; "i'm": 0.30; 'class': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'there': 0.35; "i'll": 0.36; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'skip:y 20': 0.60; 'map': 0.64; 'obvious': 0.74; 'remember,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: my favorite line of py code so far Date: Sat, 09 Nov 2013 08:12:25 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849ee7.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383981142 news.xs4all.nl 15865 [2001:888:2000:d::a6]:44075 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58906 Peter Cacioppi wrote: > my fav so far is this > > _ = lambda c : lambda x : c(*x) > > c can be any calleable and x any iterable, but I tend to use it with a > class, and then map _(class) over the result of a zip. > > It must be in the library somewhere, but I haven't found it. I'm never > sure what to call it, so I just reroll it into _ as needed. It's pretty > easy for me to remember, but I'll probably tattoo it on my chest just in > case. You mean >>> class P: ... def __init__(self, x, y): ... self.x = x ... self.y = y ... def __repr__(self): ... return "Point(x={0.x}, y={0.y})".format(self) ... >>> P(1, 2) Point(x=1, y=2) >>> _ = 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)] there is also itertools.starmap(): >>> from itertools import starmap >>> list(starmap(P, zip([1,2,3], [6, 5, 4]))) [Point(x=1, y=6), Point(x=2, y=5), Point(x=3, y=4)]