From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christian Moe Subject: Re: Python Code Block error with header option :results output Date: Sat, 01 Feb 2014 07:56:01 +0100 Message-ID: References: <1391187052.2534.12.camel@raven-CM5571> <1391203457.2534.18.camel@raven-CM5571> <87zjmbu98e.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:52855) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W9USr-0004YL-Ab for emacs-orgmode@gnu.org; Sat, 01 Feb 2014 01:54:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W9USi-0004g5-Io for emacs-orgmode@gnu.org; Sat, 01 Feb 2014 01:54:05 -0500 Received: from mail-forward5.uio.no ([2001:700:100:10::37]:49948) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W9USi-0004YL-83 for emacs-orgmode@gnu.org; Sat, 01 Feb 2014 01:53:56 -0500 Received: from exim by mail-out5.uio.no with local-bsmtp (Exim 4.80.1) (envelope-from ) id 1W9URu-0005OV-1G for emacs-orgmode@gnu.org; Sat, 01 Feb 2014 07:53:06 +0100 In-reply-to: <87zjmbu98e.fsf@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Eric Schulte Cc: Soapy Smith , emacs-orgmode@gnu.org Eric Schulte writes: > Soapy Smith writes: > >> Hello- >> >> I am not a user of Python (yet). During a comparison of code block >> behavior between Clojure and Python, I discovered a possible Python >> error. >> >> Here is the code block: >> >> #+begin_src python :results output >> a = (1, 2, 3, 4) >> return a >> #+end_src >> >> The evaluation of this block results in this error: >> >> File "", line 2 >> SyntaxError: 'return' outside function >> >> Could someone please comment if this is expected Python behavior? >> > > You should only use "return" like that in a python code block when you > have ":results value". To expand briefly on that (since this can be perplexing): The need to use `return' at all when returning the value is a special quirk of Python code blocks, and does not apply to (any?) other languages. As explained on Worg, with `:results value' (the default), "Internally, the value is obtained by wrapping the code in a function definition in the external language, and evaluating that function. Therefore, code should be written as if it were the body of such a function. In particular, note that Python does not automatically return a value from a function unless a return statement is present, and so a ‘return’ statement will usually be required in Python." http://orgmode.org/manual/Results-of-evaluation.html So when using `:results output', using `return' causes an error because there is no function to return anything from. Yours, Christian