Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'float': 0.05; 'executed': 0.07; 'input,': 0.09; 'throw': 0.09; 'tuple': 0.09; 'yeah,': 0.09; 'def': 0.10; 'assume': 0.11; 'language': 0.14; 'c):': 0.16; 'code),': 0.16; 'declaration': 0.16; 'docstring': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'marker': 0.16; 'multiplied': 0.16; 'permits': 0.16; 'quadratic': 0.16; 'sees': 0.16; 'mon,': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'feb': 0.19; 'variable': 0.20; "i'd": 0.22; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; '0.5': 0.29; 'probably': 0.29; 'code': 0.31; '(and': 0.32; 'skip:- 10': 0.32; 'comments': 0.33; 'says': 0.33; 'docs': 0.33; 'to:addr:python- list': 0.33; 'equal': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'serve': 0.36; 'but': 0.36; 'useful': 0.36; 'level': 0.37; 'two': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'supports': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'identify': 0.61; 'ever': 0.63; 'times': 0.63; 'details': 0.63; 'more': 0.63; 'therefore': 0.65; 'results': 0.65; 'special': 0.73; '2013': 0.84; 'adorn': 0.84; 'determinant': 0.84; 'seldom': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=4gi+kkML5z/YQaU4q8HXcaKMGlptn4BkHuqc+M3glMs=; b=hCrRcsDU7gpFjFyLl6eGuPcTBkFICL6aAe13xMx7gHKnyE5lKqgtyBiYUlXfzPJAdM 9qaNjhneN6SyaR+mPU1TFs+IWjPyDB8kLWMgcncwM5x72SsZksUVvM27r2YB8HJIMG+W QnMvzY+qPR02Xy/fUAjqFvwZQQ9jWFGG5kj0fmPaTapKxTYlpcDDQOHX43x3VKpisbFX KXj16x8gFgi0Ms0+XY15eA+pyObtRVM9FJyRk+qNPZbbl3I9L3gMaxYUjqhNBfzbtVft OqBOCtjf5spDok5ovbbFN0rQtT2eXQA4xJ0h4APAOGnYsRraD5Gdsbxm19Z468mg3BXq tIwA== MIME-Version: 1.0 X-Received: by 10.52.37.109 with SMTP id x13mr8053403vdj.10.1361743715022; Sun, 24 Feb 2013 14:08:35 -0800 (PST) In-Reply-To: References: <5127848B.1060004@gmail.com> <928d2cf7-728b-4f35-b8c9-4c9b958507e5@googlegroups.com> <8eadd52c-d533-4333-8c7f-7bf3a6d7b046@googlegroups.com> <9p1ii899tkalvfd1cl7sneoqa9t1cqh4oj@invalid.netcom.com> <5129482F.3080402@gmail.com> <512a5199$0$29998$c3e8da3$5496439d@news.astraweb.com> <5a3bf25b-a08b-4084-a940-e1fd05a1045d@googlegroups.com> Date: Mon, 25 Feb 2013 09:08:34 +1100 Subject: Re: Python Newbie From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361743717 news.xs4all.nl 6851 [2001:888:2000:d::a6]:52641 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39803 On Mon, Feb 25, 2013 at 8:35 AM, Joshua Landau wrote: > def solve_quadratic(a, b, c): > """Solve a quadratic equation of the form ax=B2 + bx + c =3D 0 > > The result will be a tuple of the two results; the results can be equal i= f > the determinant is 0. > This supports imaginary results for if the determinant is negative.""" > ... > results =3D [top/(2*a) for top in fraction_tops] Yeah, I think we know which one is the more readable... Just to nit-pick a little though, that returns a list when its docstring says it'll return a tuple :) Other than that (which is probably better solved by changing the docs than the code), the only change I'd make would be to ditch the fraction_tops temporary (and to throw out some of the comments that serve only to reexpress the code that immediately follows them, though for a demo they're entirely appropriate). Even in a language with mandatory declarations, the code would look pretty similar: # Assume that the declaration 'complex' permits a float - otherwise you need a Pike-style piped declaration eg "float|complex" # Details elided for brevity, keep the docstring and comments from the above version list(complex) solve_quadratic(float a, float b, float c): float determinant =3D b**2 - 4*a*c complex sqrt_determinant =3D determinant ** 0.5 tuple(complex) squareroots =3D sqrt_determinant, -sqrt_determinant return [(-b + d)/(2*a) for top in squareroots] Variable names seldom if ever need to identify their types, if by "type" you mean what the language sees as a type. There are times when it's useful to adorn a variable name with a unit, perhaps (length_ft and height_m shouldn't be multiplied together), or some other special marker (a "tainted" flag on all data that's come from user input, and which therefore must not be executed or interpolated into SQL or anything), but this is a much higher level of abstraction. ChrisA