Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'subject:Python': 0.06; 'see.': 0.07; '"if': 0.09; 'back.': 0.09; 'function,': 0.09; 'integral': 0.09; 'subject:into': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; '(int': 0.16; 'block.': 0.16; 'both,': 0.16; 'chris,': 0.16; 'corresponds': 0.16; 'default:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hides': 0.16; 'mean,': 0.16; 'object()': 0.16; 'original:': 0.16; 'quoted': 0.16; 'selects': 0.16; 'subject:variable': 0.16; 'switch.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'define': 0.26; 'posts': 0.26; 'switch': 0.26; 'second': 0.26; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; '[1]': 0.29; 'generally': 0.29; "doesn't": 0.30; '[2]': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '(on': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'figure': 0.32; 'guess': 0.33; 'skip:_ 10': 0.34; "can't": 0.35; 'something': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'branch': 0.38; 'pm,': 0.38; 'that,': 0.38; 'explain': 0.39; 'sure': 0.39; 'how': 0.40; 'expression': 0.60; 'subject:Can': 0.60; 'then,': 0.60; 'times': 0.62; 'show': 0.63; 'more': 0.64; 'mar': 0.68; 'default': 0.69; 'potentially': 0.81; 'presumably': 0.84; 'tone': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=6mtg+69zkhqh27u93hmW2+SFQVWI3G1mEvUGf7czbno=; b=ULuPd2bvUoI3kNaHK5VTf3BRK10DADapAvlg3S8FJUX+O3oUVrNqHgCRWnIaKPsqIB ShrXnIjE14in5nsHXDX3vwObX+bzVpzMsa1rxoGZkItWjTazkTtU5DZ7okGf7RFuhrET fjZSLOd8JT3EFdu2af6DS8opM0L67e4etOGMpZ8gT+OZLP8ShqM1KfOPOmRQ72omApiO cz7s3ojzE0L8lxz6bWNvcEGlyYqImu3c6Gdv/Sq011SSv1NUXM8O7b/LgdfDIOIBldmY uxLqjI8BuM1Ly3E/3ApzIVWYZgMajuBdHxLQhucnbw3h38Z+HQB1d8GstEDINhv8ltdA z8TA== MIME-Version: 1.0 X-Received: by 10.66.164.229 with SMTP id yt5mr8112800pab.67.1393655055551; Fri, 28 Feb 2014 22:24:15 -0800 (PST) In-Reply-To: <2633c4d3-e8cc-47fa-b22a-48dab2ef1a29@googlegroups.com> References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> <5f4f5a5f-327a-4616-8235-17ee9e74c488@googlegroups.com> <530fef58$0$11113$c3e8da3@news.astraweb.com> <871tynznpd.fsf@elektro.pacujo.net> <53104798$0$11113$c3e8da3@news.astraweb.com> <87ha7jy2qs.fsf@elektro.pacujo.net> <5310b031$0$29985$c3e8da3$5496439d@news.astraweb.com> <87iorzf4ro.fsf@elektro.pacujo.net> <877g8ff3hn.fsf@elektro.pacujo.net> <87wqgfdk6a.fsf@elektro.pacujo.net> <0b414429-74ee-45dd-9465-c87e98c362f2@googlegroups.com> <4bd11eec-395a-4484-a991-9fabf12328e1@googlegroups.com> <2633c4d3-e8cc-47fa-b22a-48dab2ef1a29@googlegroups.com> Date: Sat, 1 Mar 2014 17:24:15 +1100 Subject: Re: Can global variable be passed into Python function? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393655058 news.xs4all.nl 2880 [2001:888:2000:d::a6]:37604 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67297 On Sat, Mar 1, 2014 at 4:30 PM, Mark H. Harris wrot= e: > hi Chris, yeah... well think again of the switch block in C... the sw= itch block selects a branch based on an integral number (int character) tha= t is generally a return code from a function. The function hides all of th= at logic. The function runs and returns a "number" which is passed to the s= witch block. That number generally corresponds to a DEFINE constant at the = top or in the header... so we get something really readable: > > x =3D somefunc() > switch (x): > case: CONSTANT1 > call blah blah > case: CONSTANT2 > call blah blah blah > default > blah Okay. So you'd do that to make this more readable. Here's the original: if (n**2 < D(1)): a =3D __atan__(n) elif (n =3D=3D D(1)): a =3D gpi/4 elif (n =3D=3D D(-1)): a =3D -(gpi/4) elif (n < D(-1)): a =3D __atan__Lt_neg1__(n) else: a =3D __atan__Gt_1__(n) Now let's see. Your code doesn't perfectly translate, so I'm having to guess a bit here. GT_1 =3D object() LT_n1 =3D object() IS_1 =3D object() IS_n1 =3D object() def categorize(n): if (n**2 < D(1)): return None elif (n =3D=3D D(1)): return IS_1 elif (n =3D=3D D(-1)): return IS_n1 elif (n < D(-1)): return LT_n1 else: return GT_1 switch categorize(x): case GT_1: __atan__Gt_1__(x) # Not sure which you mean, this case LT_n1: __atan__Lt_neg1__(x) # or this case LT_1: __atan__Lt_1__(x) # So I have both, ish. case IS_1: a =3D gpi/4 case IS_n1: a =3D -gpi/4 default: # Presumably this should be a=3D? __atan__(x) Please copy and paste this, and then edit it so the two actually do exactly the same thing. And then, if you please [1], explain to me how the second one is more readable. It still has the exact same if/elif tree, because there's no other way to figure out which constant you should have for the switch; and as well as that, it exhibits an appalling degree of Soft Coding[2], and it's roughly three times as much code. If you want to change anything, you potentially have to edit three places: the list of constants at the top, the condition function, and the switch. This can't be your idea of readability. Show me where I'm wrong. ChrisA [1] Steven D'Aprano unwittingly quoted HMS Pinafore a few posts back. I'm now doing it consciously, for I hold that (on the seas) the expression "if you please" a particularly gentlemanly tone implants. [2] http://thedailywtf.com/Articles/Soft_Coding.aspx