Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: Procedures and functions [was Re: Why not allow empty code blocks?] Date: Sun, 31 Jul 2016 00:26:17 -0400 Lines: 45 Message-ID: References: <579B1ABB.1070500@rece.vub.ac.be> <579c2095$0$22142$c3e8da3$5496439d@news.astraweb.com> <579cbded$0$1605$c3e8da3$5496439d@news.astraweb.com> <758a1961-00bc-4b1c-834b-a7deca94d029@googlegroups.com> <579d7141$0$1606$c3e8da3$5496439d@news.astraweb.com> <20160731000133.276c46f0@imp> <1469939177.1081035.681545073.1E128CBD@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de I5yLyiiBzFZZtmYiRKAMJQnZr4nZ+datLulFQ/hSMr7g== 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; 'static': 0.03; 'error:': 0.05; 'run-time': 0.05; 'compile-time': 0.07; 'subject:code': 0.07; 'variable,': 0.07; 'assigning': 0.09; 'beginners': 0.09; 'contexts': 0.09; 'none"': 0.09; 'operator,': 0.09; 'received:internal': 0.09; 'subject:Why': 0.09; 'python': 0.10; 'language,': 0.11; 'subject:not': 0.11; 'syntax': 0.13; 'languages,': 0.15; '2016': 0.16; 'bugs.': 0.16; 'bytecode': 0.16; 'comma': 0.16; 'discarded.': 0.16; 'example)': 0.16; 'hypothetical': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'mylist': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:10.202.2.212': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'runtime.': 0.16; 'stack)': 0.16; 'string).': 0.16; 'subject:allow': 0.16; 'wrote:': 0.16; 'resolved': 0.18; 'variable': 0.18; '(the': 0.22; '31,': 0.22; 'converted': 0.22; 'explicit': 0.22; 'latter': 0.22; 'object.': 0.22; 'nearly': 0.23; 'header:In-Reply-To:1': 0.24; 'error': 0.27; 'checking': 0.27; 'define': 0.27; 'separate': 0.27; '(such': 0.27; 'object,': 0.27; '(it': 0.29; 'procedures.': 0.29; 'sites.': 0.29; 'subject: [': 0.29; 'tail': 0.29; 'handled': 0.29; 'raise': 0.29; '(including': 0.30; 'compared': 0.30; 'error.': 0.31; 'operations': 0.31; 'probably': 0.31; 'certain': 0.31; 'statement': 0.32; 'though,': 0.32; 'source': 0.33; "d'aprano": 0.33; 'operations.': 0.33; 'steven': 0.33; 'running': 0.34; 'add': 0.34; 'that,': 0.34; 'could': 0.35; 'protocol': 0.35; 'requiring': 0.35; 'returning': 0.35; 'level': 0.35; 'but': 0.36; 'instead': 0.36; 'assigned': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'method': 0.37; 'received:66': 0.38; 'end': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'called': 0.40; 'some': 0.40; 'header:Message-Id:1': 0.61; 'side': 0.62; 'real': 0.62; 'leaving': 0.63; 'different': 0.63; 'within': 0.64; 'jul': 0.72; 'obvious': 0.76; '__call__': 0.84; '+1000': 0.91; 'mistake': 0.91; 'write:': 0.91; 'mystery': 0.95 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=JcoLbC6s7queG0T6GnVpRH8bt84=; b=b7F/W6 xCSfh4R4/x/hBkhY3gCoGiK+9hHA1sNsnVvsHpt5VYY7hIy9pj9xWsfG3zUPOdJA zvK7qqT0nSGbzsWi2YgQtUuO25qw431xcG61yivXQ/101qXVY/T+v915BQboqZbH xKAjZ9CMhuR2IjLz64bsbVYBbZg/E1t+OCIfU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=JcoLbC6s7queG0T 6GnVpRH8bt84=; b=LsKZmAVoGI3EyYLf1t9iCGIsGnQSuuoHJPDnb2Ouz89n1OL Ofi4M4wC5GmJLjBezO0QomBUCQ+XhcF2MsyzOemFmycvjnEG1B+j1qVhwE9JE5HH ilFf6wG2xtUvmRPzYad5PqpfM4F5lRfjFqdEnXDAhK4yEOs/hpCF8VnJYzg8= X-Sasl-Enc: GH2VczMhau3MD68fo4mhjxDbOkNP9dUJ1vWArsbcu00L 1469939177 X-Mailer: MessagingEngine.com Webmail Interface - ajax-501c3c37 In-Reply-To: <20160731000133.276c46f0@imp> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1469939177.1081035.681545073.1E128CBD@webmail.messagingengine.com> X-Mailman-Original-References: <579B1ABB.1070500@rece.vub.ac.be> <579c2095$0$22142$c3e8da3$5496439d@news.astraweb.com> <579cbded$0$1605$c3e8da3$5496439d@news.astraweb.com> <758a1961-00bc-4b1c-834b-a7deca94d029@googlegroups.com> <579d7141$0$1606$c3e8da3$5496439d@news.astraweb.com> <20160731000133.276c46f0@imp> Xref: csiph.com comp.lang.python:112122 On Sun, Jul 31, 2016, at 00:01, D'Arcy J.M. Cain wrote: > On Sun, 31 Jul 2016 13:32:16 +1000 > Steven D'Aprano wrote: > > Many beginners make the mistake of writing: > > > > mylist = mylist.sort() > > > > or try to write: > > > > mylist.sort().reverse() > > > > If we had procedures, that would be an obvious error (possibly even a > > compile-time syntax error) instead of a perplexing source of mystery > > bugs. > > While neither is a syntax error, the latter is definitely a run-time > error: The only way to make it a syntax error would be to have a different syntax for *calling* procedures. Barring that, all you can do is make the first one a run-time error. As Python is a dynamic language, mylist.sort is an object which is resolved at runtime. When you call this object, its __call__ method is called and returns the None object. This object is valid for very few operations (it can be assigned to a variable [or returned], treated as a boolean, compared for equality, or converted to a string). In some other languages, procedures have a static return type (often called "void") which is valid for even fewer operations. You can use it as a statement expression or within certain contexts (the left side of C's comma operator, for example) where the value of an expression is always discarded. In a hypothetical Python with procedures but no separate syntax for calling them, you would need to define the __call__ protocol in a way that it can indicate that the called object is a procedure. Though, it could probably be handled at the python level by just allowing "return" or running off the end to do this, requiring "return None" for the explicit return of a None object.. The real cost is, it would require that any operation (such as assigning as a variable, checking it as a boolean, etc) other than discarding it (including returning it, so you'd need extra bytecode to check rather than merely leaving tail returns on the stack) check immediately raise an error. This would add extra processing to nearly all call sites.