Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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; 'python,': 0.02; 'example:': 0.03; 'else:': 0.03; 'elif': 0.05; 'subject:Python': 0.06; 'explicit': 0.07; 'clause': 0.09; 'function,': 0.09; 'instance.': 0.09; 'lookup': 0.09; 'matched': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; '"="': 0.16; '*you*': 0.16; 'compatible,': 0.16; 'dict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'loop.': 0.16; 'once.': 0.16; 'operator.': 0.16; 'readability': 0.16; 'readable': 0.16; 'subject:variable': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'pointed': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; "haven't": 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'compare': 0.26; 'switch': 0.26; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; 'getting': 0.31; "skip:' 10": 0.31; '(my': 0.31; 'another.': 0.31; 'fine,': 0.31; 'there.': 0.32; 'compatible': 0.32; 'another': 0.32; 'table': 0.34; "i'd": 0.34; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'false': 0.36; 'subject:?': 0.36; 'changing': 0.37; 'list.': 0.37; 'implement': 0.38; 'track': 0.38; 'supporting': 0.39; 'either': 0.39; 'called': 0.40; 'how': 0.40; 'is.': 0.60; 'subject:Can': 0.60; 'truly': 0.60; 'simply': 0.61; 'back': 0.62; 'show': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'mar': 0.68; 'anything.': 0.68; 'statement,': 0.68; 'special': 0.74; 'dict.': 0.84; 'to:none': 0.92; 'remember,': 0.93 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; bh=CgzxX0WMrnoaDg7gpqEs/8E1Xz3YwGpzZk+O7mOU88o=; b=hHQAG/NszcHZ6HM3Whdc1CWY3Uy+rhqD6yLcsGmTFjfz/z7DgE/qMxygAW9Lb26YVe k6s+uqRV4StF3T5StN/LW8MPGLY7XI47pll6sZ29JaKI/xTl8Gra0fkgLEWyssZcAm/C GxEiRnbOl9UB4IVn/xmWhEYbYunqMXS2eIJkU+WOk4FRw8lAouvYVC0NiTxnXIt+e587 kxkgYuVDN5hmNClXZiTng6bnJq+6yO9w8/MxZxfaDWsSnuy+7KewKJBjo70BFJSKex7j CM1cMWuT+t0bVQrcuwuGj+oLXQV2PdLmPTUT1voBFua2s/rRurlCSPQJEsp6T1dT+5W7 F0ww== MIME-Version: 1.0 X-Received: by 10.68.197.36 with SMTP id ir4mr7159712pbc.46.1393638621280; Fri, 28 Feb 2014 17:50:21 -0800 (PST) In-Reply-To: <877g8ff3hn.fsf@elektro.pacujo.net> 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> Date: Sat, 1 Mar 2014 12:50:21 +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 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393638625 news.xs4all.nl 2915 [2001:888:2000:d::a6]:56120 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67279 On Sat, Mar 1, 2014 at 6:20 AM, Marko Rauhamaa wrote: > Your example: > > compare_key = { > # Same target(s). > ast.Assign: lambda node: ' '.join(dump(t) for t in node.targets), > # Same target and same operator. > ast.AugAssign: lambda node: dump(node.target) + dump(node.op) + "=", > # A return statement is always compatible with another. > ast.Return: lambda node: "(easy)", > # Calling these never compatible is wrong. Calling them > # always compatible will give lots of false positives. > ast.Expr: lambda node: "(maybe)", > # These ones are never compatible, so return some > # object that's never equal to anything. > ast.Import: lambda node: float("nan"), > ast.ImportFrom: lambda node: float("nan"), > ast.Pass: lambda node: float("nan"), > ast.Raise: lambda node: float("nan"), > ast.If: lambda node: float("nan"), > } > > vs (my proposal): > > with key from ast: > if Assign: > return ' '.join(dump(t) for t in node.targets) > elif AugAssign: > # Same target and same operator. > return dump(node.target) + dump(node.op) + "=" > elif Return: > # A return statement is always compatible with another. > return "(easy)" > elif Expr: > # Calling these never compatible is wrong. Calling them > # always compatible will give lots of false positives. > return "(maybe)" > else: > # These ones are never compatible, so return some > # object that's never equal to anything. > return float("nan") > > Which do *you* find more readable? Your proposal requires that I wrap the whole thing up in a function, or else pass the lookup key to my function every time and switch on it, instead of getting back a single function once. So you haven't truly matched the functionality, which means it's hard to compare readability - I'd have to also look at how readable the call site is, and that's definitely going to be penalized. Remember, I pointed out that the function gets called more than once - once on the special case and then once in a loop. Plus, how do you implement mutability? The else clause needs to do a one-off print to stderr, so either the dispatch table needs to be changed, or the else clause needs some other system of keeping track of its previously-seen list. Additionally, I have another script that actually monkey-patches the Expr case out, simply by changing the dict. It works just fine, because, again, the dict is mutable. To do that with a switch statement, I'd need to put an explicit 'if' into there. Show me that version and let's see how readable it is. There may be a case (pun intended) for adding a switch block to Python, but this isn't a strong supporting instance. ChrisA