Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!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; 'true,': 0.04; 'subject:Python': 0.05; 'none,': 0.05; 'run-time': 0.05; '*not*': 0.07; 'default.': 0.07; 'exception.': 0.07; 'wrapper': 0.07; 'cc:addr:python-list': 0.09; 'abort': 0.09; 'c-level': 0.09; 'lambda:': 0.09; 'meaningful': 0.09; 'result;': 0.09; 'thrown': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'interpreter': 0.15; 'producing': 0.15; 'result.': 0.15; 'value.': 0.15; '*always*': 0.16; 'fail,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "isn't.": 0.16; 'operation.': 0.16; 'path:': 0.16; 'raised;': 0.16; 'redundant.': 0.16; 'return,': 0.16; 'stops.': 0.16; 'unbound,': 0.16; 'wrote:': 0.16; 'module,': 0.18; 'result,': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'context.': 0.22; 'exec': 0.22; 'produces': 0.22; 'sep': 0.22; 'am,': 0.23; 'bit': 0.23; 'seems': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'hosting': 0.25; "doesn't": 0.26; 'command': 0.26; 'error': 0.27; 'define': 0.27; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'operations,': 0.27; 'function': 0.28; 'raise': 0.29; 'code': 0.30; "i'd": 0.31; 'statement': 0.32; 'run': 0.33; 'point': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'raising': 0.33; 'rule': 0.33; 'steven': 0.33; 'definition': 0.34; 'except': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'could': 0.35; 'false': 0.35; 'returning': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'instead': 0.36; 'possible': 0.36; '(and': 0.36; '(3)': 0.36; 'evaluation': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'operating': 0.37; 'say': 0.37; '(2)': 0.37; 'itself': 0.38; '(1)': 0.38; 'wrong': 0.38; 'why': 0.39; 'does': 0.39; 'system.': 0.39; 'rather': 0.39; 'information,': 0.61; 'caused': 0.61; 'matter': 0.63; 'more': 0.63; 'to,': 0.63; '"it': 0.84; '"yield': 0.84; 'chrisa': 0.84; "it'd": 0.84; 'or:': 0.84; 'triggering': 0.84; 'absolutely': 0.88; 'to:none': 0.91; 'hardest': 0.91; 'imagine': 0.96 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=h5R0e6ua/UVUp7MZTi7YZjtLlvh7CckSP+uRQoRPOGA=; b=g8deBQw4fu2xI2rL/hlWDFj2pIXETHFuKye2xYk72al0WkGnP83BS1HOTVUAcPLLN6 Zp5ikx3GIGM/uTtKXfVB/jra4jm2/X+c31/RpjUAtz+6UsNuQx5j75yZLHdAvM6+HJmV 5wC/ZxdlDdOXnQH9jd28z5JLgY1rx5JRD6jEF+lQHOJmBlKRORVzAqwjyv4H1cK2qaUM xD9FRHrolHHQTq4ZUSzEF+6H9Dc6hYoJONT/FasklYdF1Wbgp8dur0NGQwpfi5d5Mix8 V1SHsDLi2o6tYnOS3VcRiMwhg8+rYtop0PYOKcnKqxtyPQHnjqQ0b7t15LumqW2iLBIs FPZA== MIME-Version: 1.0 X-Received: by 10.50.3.66 with SMTP id a2mr9105578iga.92.1441910023071; Thu, 10 Sep 2015 11:33:43 -0700 (PDT) In-Reply-To: <55f1c85d$0$1638$c3e8da3$5496439d@news.astraweb.com> References: <86fa425b-d660-45ba-b0f7-3beebdec8e14@googlegroups.com> <55EE9EEC.1060907@rece.vub.ac.be> <55EEDD37.5090602@gmx.com> <55f072aa$0$1669$c3e8da3$5496439d@news.astraweb.com> <55f1a8df$0$1660$c3e8da3$5496439d@news.astraweb.com> <55f1c85d$0$1638$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 11 Sep 2015 04:33:42 +1000 Subject: Re: Python handles globals badly. 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.20+ 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441910026 news.xs4all.nl 23842 [2001:888:2000:d::a6]:44814 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96283 On Fri, Sep 11, 2015 at 4:13 AM, Steven D'Aprano wrote: > Sometimes they might be. But in general, I think it is meaningless to expect > a imperative command to have a return result. The whole point of an > imperative command is that it operates by side-effect. > > For example, what would `del x` return if it were an expression instead of a > statement? I can think of two reasonable alternatives, but both feel a bit > wrong to me. > > (1) Return True if x was successfully unbound, False if it wasn't. Except > that raising an exception seems more Pythonic, in which case returning True > seems redundant. It will *always* return True, unless there's an exception. > So why bother? We only bother because there's no way to *not* return a > result if "everything is an expression". > > (2) Return None, like functions do by default. But again, it only returns > None, not because None is a meaningful thing to return, but because we > don't actually have a way to say "it doesn't return anything". Yes, or: (3) Return the old value that x contained, the way dict.pop() does. That makes it a "destructive retrieval" rather than simply a destruction operation. > Or os.abort. The docs for that say: > > Help on built-in function abort in module posix: > > abort(...) > abort() -> does not return! > > Abort the interpreter immediately. This 'dumps core' or otherwise fails > in the hardest way possible on the hosting operating system. > > > So, what would os.abort() return, if everything is an expression? Since this is in the os module, and is thus a thin wrapper around lower-level operations, I could imagine it returning an error code (the way the C-level exec functions do - if they succeed, they don't return, but if they fail, they return an error number); in Python, it'd be best to have it either abort the process (and thus not return), or raise an exception (and thus not return). But abort functions are a rarity. It's not a problem to have a rule "all functions must return something" (as Python does), and then have functions that never actually use the normal return path: def raise_(exc): raise exc raise_stopiteration = iter([]).__next__ raise_typeerror = lambda: ""() Calling one of these functions is *syntactically* an expression, but at run time, it'll never actually get as far as producing a value. If it's at all possible for the operation to, based on run-time information, NOT abort, then it absolutely has to be able to return. > Because that's the definition of an expression in this context. An > expression is evaluated to either return a result, or raise an exception. I'd define it more simply: An expression always produces a result. It's possible that, during the evaluation of an expression, an exception will be raised; if that happens, evaluation stops. Doesn't matter whether the expression itself caused that, or if an OS-level signal did (eg triggering KeyboardInterrupt), or if the expression was "yield 1" and an exception got thrown in. But yes, an expression is something that generates a result; a statement isn't. ChrisA