* 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; 11+ 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] 11+ 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-10-13 11:41 ` Ihor Radchenko
2024-09-07 9:27 ` Sébastien Gendre
1 sibling, 1 reply; 11+ 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] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-09-07 8:12 ` Dr. Arne Babenhauserheide
@ 2024-10-13 11:41 ` Ihor Radchenko
2024-10-20 7:08 ` Jack Kamm
0 siblings, 1 reply; 11+ messages in thread
From: Ihor Radchenko @ 2024-10-13 11:41 UTC (permalink / raw)
To: Dr. Arne Babenhauserheide; +Cc: Rudolf Adamkovič, emacs-orgmode
"Dr. Arne Babenhauserheide" <arne_bab@web.de> 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
Yes, patch would be welcome.
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-10-13 11:41 ` Ihor Radchenko
@ 2024-10-20 7:08 ` Jack Kamm
2024-10-20 9:39 ` Ihor Radchenko
0 siblings, 1 reply; 11+ messages in thread
From: Jack Kamm @ 2024-10-20 7:08 UTC (permalink / raw)
To: Ihor Radchenko, Dr. Arne Babenhauserheide
Cc: Rudolf Adamkovič, emacs-orgmode
Ihor Radchenko <yantar92@posteo.net> writes:
>> 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
>
> Yes, patch would be welcome.
As Sebastian mentioned, Worg already documents the behavior, under the
section "Return values":
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html#org6eb75c6
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-10-20 7:08 ` Jack Kamm
@ 2024-10-20 9:39 ` Ihor Radchenko
2024-10-21 6:00 ` Jack Kamm
0 siblings, 1 reply; 11+ messages in thread
From: Ihor Radchenko @ 2024-10-20 9:39 UTC (permalink / raw)
To: Jack Kamm; +Cc: Dr. Arne Babenhauserheide, Rudolf Adamkovič, emacs-orgmode
Jack Kamm <jackkamm@gmail.com> writes:
>>> So I think WORG needs to be fixed.
>>>
>>> Can you send a patch?
>>> https://git.sr.ht/~bzg/worg
>>
>> Yes, patch would be welcome.
>
> As Sebastian mentioned, Worg already documents the behavior, under the
> section "Return values":
>
> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html#org6eb75c6
Right.
Then, I see nothing else that can be done on Org side.
I'm afraid that trying to replicate Python REPL behavior for
non-interactive src blocks will be beyond what we can reasonably do in
Org: we would somehow need to detect the last statement in the src
blocks, which may or may not span a single line; and multiline statements
will need to use some kind of Python parser. All this sounds way too
fragile to be practical.
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-10-20 9:39 ` Ihor Radchenko
@ 2024-10-21 6:00 ` Jack Kamm
2024-10-21 18:03 ` Ihor Radchenko
0 siblings, 1 reply; 11+ messages in thread
From: Jack Kamm @ 2024-10-21 6:00 UTC (permalink / raw)
To: Ihor Radchenko
Cc: Dr. Arne Babenhauserheide, Rudolf Adamkovič, emacs-orgmode
Ihor Radchenko <yantar92@posteo.net> writes:
> I'm afraid that trying to replicate Python REPL behavior for
> non-interactive src blocks will be beyond what we can reasonably do in
> Org: we would somehow need to detect the last statement in the src
> blocks, which may or may not span a single line; and multiline statements
> will need to use some kind of Python parser. All this sounds way too
> fragile to be practical.
Well, ob-python sessions implement this sort of parser in
`org-babel-python-format-session-value', which AFAIK is robust.
I think it would be feasible to switch non-session blocks to use this
parser, which IMO is nicer behavior -- the "return" statements always
felt weird to me.
But a major reason not to do it is to avoid breaking compatibility. Lots
of old ob-python nonsession blocks with "return" statements out there.
But perhaps we could implement an option to switch nonsession value to
the session value behavior (omitting "return"). The default behavior
should remain unchanged (requiring "return"), but in the long term we
could consider changing the default if there is consensus for it.
(I likely don't have bandwidth to implement such an option in the near
term, but would be supportive if anyone wants to have a go at it).
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-10-21 6:00 ` Jack Kamm
@ 2024-10-21 18:03 ` Ihor Radchenko
2024-10-22 2:43 ` Jack Kamm
0 siblings, 1 reply; 11+ messages in thread
From: Ihor Radchenko @ 2024-10-21 18:03 UTC (permalink / raw)
To: Jack Kamm; +Cc: Dr. Arne Babenhauserheide, Rudolf Adamkovič, emacs-orgmode
Jack Kamm <jackkamm@gmail.com> writes:
> Well, ob-python sessions implement this sort of parser in
> `org-babel-python-format-session-value', which AFAIK is robust.
Interesting. I did not remember this.
> I think it would be feasible to switch non-session blocks to use this
> parser, which IMO is nicer behavior -- the "return" statements always
> felt weird to me.
>
> But a major reason not to do it is to avoid breaking compatibility. Lots
> of old ob-python nonsession blocks with "return" statements out there.
May we detect if there is a return statement?
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-10-21 18:03 ` Ihor Radchenko
@ 2024-10-22 2:43 ` Jack Kamm
2024-10-23 17:13 ` Ihor Radchenko
0 siblings, 1 reply; 11+ messages in thread
From: Jack Kamm @ 2024-10-22 2:43 UTC (permalink / raw)
To: Ihor Radchenko
Cc: Dr. Arne Babenhauserheide, Rudolf Adamkovič, emacs-orgmode
Ihor Radchenko <yantar92@posteo.net> writes:
>> I think it would be feasible to switch non-session blocks to use this
>> parser, which IMO is nicer behavior -- the "return" statements always
>> felt weird to me.
>>
>> But a major reason not to do it is to avoid breaking compatibility. Lots
>> of old ob-python nonsession blocks with "return" statements out there.
>
> May we detect if there is a return statement?
That's a nice idea, I like it.
Probably there should be a customization
`org-babel-python-nonsession-return-style', which could take on the
values 'require-return (old behavior), 'last-statement (session style),
or 'auto (detect if there is return statement).
While technically a breaking change, I think 'auto would be a reasonable
default, since it would only change the behavior on the unusual edge
case where the user deliberately returned "None" by omitting a return
statement.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-10-22 2:43 ` Jack Kamm
@ 2024-10-23 17:13 ` Ihor Radchenko
0 siblings, 0 replies; 11+ messages in thread
From: Ihor Radchenko @ 2024-10-23 17:13 UTC (permalink / raw)
To: Jack Kamm; +Cc: Dr. Arne Babenhauserheide, Rudolf Adamkovič, emacs-orgmode
Jack Kamm <jackkamm@gmail.com> writes:
>> May we detect if there is a return statement?
>
> That's a nice idea, I like it.
>
> Probably there should be a customization
> `org-babel-python-nonsession-return-style', which could take on the
> values 'require-return (old behavior), 'last-statement (session style),
> or 'auto (detect if there is return statement).
+1
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
^ permalink raw reply [flat|nested] 11+ 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
2024-10-28 22:25 ` Rudolf Adamkovič
1 sibling, 1 reply; 11+ 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] 11+ messages in thread
* Re: Org Babel says 1 + 1 in Python is None
2024-09-07 9:27 ` Sébastien Gendre
@ 2024-10-28 22:25 ` Rudolf Adamkovič
0 siblings, 0 replies; 11+ messages in thread
From: Rudolf Adamkovič @ 2024-10-28 22:25 UTC (permalink / raw)
To: Sébastien Gendre; +Cc: emacs-orgmode
Sébastien Gendre <seb@k-7.ch> writes:
> But, 1 + 1 should be 11.
And by 11 you mean 10, of course [1].
> Joke aside, for Python, how you return the value depend of if you use
> a session or not. [...] Is it the article you have read on WORG?
Gotcha.
[1] "There are only 10 types of people in the world: those who
understand binary, and those who don't."
Rudy
--
"Simplicity is complexity resolved."
--- Constantin Brâncuși, 1876-1957
Rudolf Adamkovič <rudolf@adamkovic.org> [he/him]
http://adamkovic.org
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2024-10-28 22:26 UTC | newest]
Thread overview: 11+ 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-10-13 11:41 ` Ihor Radchenko
2024-10-20 7:08 ` Jack Kamm
2024-10-20 9:39 ` Ihor Radchenko
2024-10-21 6:00 ` Jack Kamm
2024-10-21 18:03 ` Ihor Radchenko
2024-10-22 2:43 ` Jack Kamm
2024-10-23 17:13 ` Ihor Radchenko
2024-09-07 9:27 ` Sébastien Gendre
2024-10-28 22:25 ` Rudolf Adamkovič
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.