emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Org Babel says 1 + 1 in Python is None
@ 2024-09-06 13:33 Rudolf Adamkovič
  2024-09-07  8:12 ` Dr. Arne Babenhauserheide
  2024-09-07  9:27 ` Sébastien Gendre
  0 siblings, 2 replies; 3+ messages in thread
From: Rudolf Adamkovič @ 2024-09-06 13:33 UTC (permalink / raw)
  To: emacs-orgmode

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
-- 
"Mathematics takes us still further from what is human into the region
of absolute necessity, to which not only the actual world, but every
possible world, must conform."  --- Bertrand Russell, 1902

Rudolf Adamkovič <rudolf@adamkovic.org> [he/him]
http://adamkovic.org


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Org Babel says 1 + 1 in Python is None
  2024-09-06 13:33 Org Babel says 1 + 1 in Python is None Rudolf Adamkovič
@ 2024-09-07  8:12 ` Dr. Arne Babenhauserheide
  2024-09-07  9:27 ` Sébastien Gendre
  1 sibling, 0 replies; 3+ messages in thread
From: Dr. Arne Babenhauserheide @ 2024-09-07  8:12 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 715 bytes --]

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).

In the org-mode documentation I read:

    For languages like Python, an explicit return statement is mandatory when using ‘:results value’.
    https://orgmode.org/manual/Results-of-Evaluation.html

So I think WORG needs to be fixed.

Can you send a patch?
https://git.sr.ht/~bzg/worg

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Org Babel says 1 + 1 in Python is None
  2024-09-06 13:33 Org Babel says 1 + 1 in Python is None Rudolf Adamkovič
  2024-09-07  8:12 ` Dr. Arne Babenhauserheide
@ 2024-09-07  9:27 ` Sébastien Gendre
  1 sibling, 0 replies; 3+ messages in thread
From: Sébastien Gendre @ 2024-09-07  9:27 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1457 bytes --]


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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 849 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-09-07  9:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-06 13:33 Org Babel says 1 + 1 in Python is None Rudolf Adamkovič
2024-09-07  8:12 ` Dr. Arne Babenhauserheide
2024-09-07  9:27 ` Sébastien Gendre

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).