From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: [PATCH] (Tiny) Tweak Python session null return value Date: Mon, 17 Feb 2020 14:45:23 -0500 Message-ID: References: <87a75hfah9.fsf@gmail.com> <877e0lf307.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000008c6fee059ecacc27" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:58266) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j3mKs-0005CV-L3 for emacs-orgmode@gnu.org; Mon, 17 Feb 2020 14:45:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j3mKr-00039J-15 for emacs-orgmode@gnu.org; Mon, 17 Feb 2020 14:45:42 -0500 In-Reply-To: <877e0lf307.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-mx.org@gnu.org Sender: "Emacs-orgmode" To: Jack Kamm Cc: Bastien , org-mode-email --0000000000008c6fee059ecacc27 Content-Type: text/plain; charset="UTF-8" I can see why you would want to see True/False there, but to get the value, you need to specifically return what you want because AFAIK the body is wrapped in a function that is evaluated to get the value, it is not simply the last thing that gets evaluated. Your example clarified to me at least why it would be tricky to figure it out, you can't rely on the last line, for example. I don't know if there is some special Python variable that contains that. It is also a little strange to me to put return specifically in like this: #+begin_src python :session :results value if some_condition: a = True else: a = False return a #+end_src or: #+BEGIN_SRC python :results value a = 1 return True if a else False #+END_SRC #+RESULTS: : True because it is not valid syntax in a script (try it with :results output), but this is how it has been for ob-python all along when what you want is the value returned. John ----------------------------------- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Mon, Feb 17, 2020 at 2:06 PM Jack Kamm wrote: > Hi John, > > John Kitchin writes: > > > I think None is correct. If you don't specify a return value in Python, > > then a function returns None. I would expect that to happen in a Python > > block too. > > Hmm, OK, thanks for your intuition, it's useful feedback. > > Working this out loud, I was considering the following sort of block: > > #+begin_src python :session :results value > if some_condition: > True > else: > False > #+end_src > > #+RESULTS: > : None > > Ideally, it would return True/False, but the current implementation > cannot grab that result unfortunately. (In its defense, it at least > doesn't crash like it did before). > > I was thinking not printing anything at all under "#+RESULTS" would be > less surprising than printing "None". But both are admittedly surprising > at first. > > I agree your preference of returning "None" is the more technically > consistent behavior though. > > I don't want to rush a decision now, I think I need to gather more user > feedback after the 9.4 release. I'll ask Bastien to revert that commit > and put this off to 9.5, rather than writing an updated NEWS entry like > he asked. > > Either way, we'll need to update Worg to clearly document what's going > on here. > --0000000000008c6fee059ecacc27 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I can see why you would want to see True/False there, but = to get the value, you need to specifically return what you want because AFA= IK the body is wrapped in a function that is evaluated to get the value, it= is not simply the last thing that gets evaluated. Your example clarified t= o me at least why it would be tricky to figure it out, you can't rely o= n the last line, for example. I don't know if there is some special Pyt= hon variable that contains that.=C2=A0

It is also a litt= le strange to me to put return specifically in like this:

#+begin_src python :session :results value
=C2=A0 if some_condit= ion:
=C2=A0 =C2=A0 =C2=A0 a =3D True
=C2=A0 else:
=C2=A0 =C2=A0 = =C2=A0 a =3D False
return a
#+end_src

or:

#+BEGIN_SRC python :results value
a =3D 1
ret= urn True if a else False
#+END_SRC

#+RESULTS:
: True
<= br>
because it is not valid syntax in a script (try it with :resu= lts output), but this is how it has been for ob-python all along when what = you want is the value returned.

John

-----------------------------= ------
Professor John Kitchin=C2=A0
Doherty Hall A207F
Department = of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 152= 13
412-268-7803

<= br>
On Mon,= Feb 17, 2020 at 2:06 PM Jack Kamm <jackkamm@gmail.com> wrote:
Hi John,

John Kitchin <jkitchin@andrew.cmu.edu> writes:

> I think None is correct. If you don't specify a return value in Py= thon,
> then a function returns None. I would expect that to happen in a Pytho= n
> block too.

Hmm, OK, thanks for your intuition, it's useful feedback.

Working this out loud, I was considering the following sort of block:

#+begin_src python :session :results value
=C2=A0 if some_condition:
=C2=A0 =C2=A0 =C2=A0 True
=C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 False
#+end_src

#+RESULTS:
: None

Ideally, it would return True/False, but the current implementation
cannot grab that result unfortunately. (In its defense, it at least
doesn't crash like it did before).

I was thinking not printing anything at all under "#+RESULTS" wou= ld be
less surprising than printing "None". But both are admittedly sur= prising
at first.

I agree your preference of returning "None" is the more technical= ly
consistent behavior though.

I don't want to rush a decision now, I think I need to gather more user=
feedback after the 9.4 release. I'll ask Bastien to revert that commit<= br> and put this off to 9.5, rather than writing an updated NEWS entry like
he asked.

Either way, we'll need to update Worg to clearly document what's go= ing
on here.
--0000000000008c6fee059ecacc27--