emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Org Babel says 1 + 1 in Python is None


From: Sébastien Gendre
Subject: Re: Org Babel says 1 + 1 in Python is None
Date: Sat, 07 Sep 2024 11:27:39 +0200
User-agent: mu4e 1.12.1; emacs 29.4

But, 1 + 1 should be 11.

Joke aside, for Python, how you return the value depend of if you use a
session or not.

By default, a Python code block do not use a session. In this case, when
executed, the code is wrapped into a Python function and then this
function is called. So, to return a value, you need to use the "return"
statement.

With your example:
  src_python{return 1 + 1}


When you use a session for your code block, the last statement value is
returned if it is a top level expression. In this case, you do not use a
"return" statement.



Is it the article you have read on WORG ?
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html

The part you have quoted maybe miss a reference to the section who
explain how the return mechanism work for values.


Best regards

-------
Gendre Sébastien


Rudolf Adamkovič <rudolf@adamkovic.org> writes:

> Today, I decided to start using Python in Org.
>
> (1) I read in "WORG":
>
>   :results {output, value}: [...] Value results are the value of the
>   last expression evaluated in the code block. Value mode is the default
>   (as with other languages).
>
> (2) So, I try it, and get:
>
>   src_python{1 + 1} {{{results(=None=)}}}
>
> I expected this to be 2.
>
> Questions:
>
> - Who is correct: (1) the documentation or (2) the implementation?
> - If (2) is correct, do I have to write `return' every time?
>
> Rudy

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]