Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:missing': 0.07; 'python': 0.08; 'received:mail-lpp01m010-f46.google.com': 0.09; 'am,': 0.12; 'def': 0.13; 'addendum:': 0.16; 'expression,': 0.16; 'idioms': 0.16; 'reusable': 0.16; 'subject:Language': 0.16; '\xa0def': 0.16; '\xa0for': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'cc:2**0': 0.26; 'value.': 0.28; 'pass': 0.29; 'message-id:@mail.gmail.com': 0.29; '24,': 0.29; 'yield': 0.29; 'cc:addr:python.org': 0.29; 'object.': 0.30; 'actually': 0.31; 'instead.': 0.32; 'received:209.85.215.46': 0.32; "i've": 0.32; '----': 0.32; 'fri,': 0.34; 'follows:': 0.34; 'done': 0.34; 'especially': 0.34; 'anything': 0.34; 'try:': 0.34; 'something': 0.35; 'switch': 0.35; '(for': 0.35; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'received:209.85': 0.38; 'forgive': 0.38; 'should': 0.38; 'sometimes': 0.38; 'except': 0.39; 'received:209.85.215': 0.39; 'received:209': 0.39; 'header:Received:6': 0.61; 'custom': 0.61; 'subject:!': 0.66; 'soon': 0.72; 'min': 0.84; 'subject:types': 0.84; '10:32': 0.91 Received-SPF: pass (google.com: domain of ian.g.kelly@gmail.com designates 10.112.10.72 as permitted sender) client-ip=10.112.10.72; Authentication-Results: mr.google.com; spf=pass (google.com: domain of ian.g.kelly@gmail.com designates 10.112.10.72 as permitted sender) smtp.mail=ian.g.kelly@gmail.com; dkim=pass header.i=ian.g.kelly@gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=ZBCGpJhD1UGRLyjAWq/i/fx/TvAeNxNIfzjh4XeG9e0=; b=n1TkE2L/+6GH/Kp2dgZUez7JVCw1ptKKt8fO602kESnjWb6yJAlGt4J47dliO0B5P/ KpTK2fNM/+SwB/ikL2tWRtSs+xOl5IGpskf+Hy87yDU/GsvhPlHrHkr6Se9V1se+W2v9 pyxxL02N8U4dfd2KIpEXPCh0oA6+ueZ1Ip24I= MIME-Version: 1.0 In-Reply-To: References: <8c2096d9-3cc2-4b64-8f11-c1d958d94243@x19g2000yqh.googlegroups.com> <9qpkvkFberU1@mid.individual.net> From: Ian Kelly Date: Fri, 24 Feb 2012 18:26:42 -0700 Subject: Re: PyWart: Language missing maximum constant of numeric types! To: Devin Jeanpierre Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330133234 news.xs4all.nl 6917 [2001:888:2000:d::a6]:38520 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20848 On Fri, Feb 24, 2012 at 10:32 AM, Devin Jeanpierre wrote: > On Fri, Feb 24, 2012 at 9:25 AM, Neil Cerutti wrote: >> The only time I've naively pined for such a thing is when >> misapplying C idioms for finding a minimum value. >> >> Python provides an excellent min implementation to use instead. > > min can be a little inconvenient. As soon as anything complicated has > to be done during the min expression, you need to switch to using > something else for sanity's sake. In that vein, I do actually > sometimes use float('inf') (for numbers), or a custom max/min object. > > ---- > > Silly and completely nonserious addendum: > > Forgive me, I have spoken in error! min is the one true way, for you > can still do it with a little wrangling, as follows: > > =A0 =A0@operator.itemgetter(1) > =A0 =A0@min > =A0 =A0@apply > =A0 =A0def closest_object(): > =A0 =A0 =A0 =A0for x in xrange(board_width) > =A0 =A0 =A0 =A0 =A0 =A0for y in xrange(board_height): > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0try: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0entity =3D board.get_entity(x, y) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0except EntityNotFound: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0pass > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0yield distance(player.pos, entity.= pos), entity Cute, but what's so terrible about: def all_entities(): for x in xrange(board_width): for y in xrange(board_height): try: yield board.get_entity(x, y) except EntityNotFound: pass closest_object =3D min(all_entities, key=3Dlambda e: distance(player.pos, e.pos)) Especially given that all_entities should be reusable in other contexts. Cheers, Ian