[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] Python Code Block error with header option :results output
From: |
Christian Moe |
Subject: |
Re: [O] Python Code Block error with header option :results output |
Date: |
Sat, 01 Feb 2014 07:56:01 +0100 |
User-agent: |
mu4e 0.9.9.5-dev6; emacs 24.3.1 |
Eric Schulte writes:
> Soapy Smith <address@hidden> 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 "<stdin>", 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
- Re: [O] Python Code Block error with header option :results output,
Christian Moe <=