emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Schulte <schulte.eric@gmail.com>
To: Rustom Mody <rustompmody@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: babel - python question
Date: Fri, 05 Jul 2013 08:28:23 -0600	[thread overview]
Message-ID: <87vc4pgjy0.fsf@gmail.com> (raw)
In-Reply-To: <CAJ+TeocRt1zOrPY3kYDDO3G1ie11ewtV-YC+mvgSB+0rJF4=jg@mail.gmail.com> (Rustom Mody's message of "Fri, 5 Jul 2013 18:14:28 +0530")

Rustom Mody <rustompmody@gmail.com> writes:

> [Complete babel noob here]
>
> Following the babel doc
> http://orgmode.org/worg/org-contrib/babel/intro.html#library-of-babel
>
> I wrote this
>
> * Head
> #+name: ppp :results value
> #+begin_src python
> import time
> print("Hello, today's date is %s" % time.ctime())
> print('Two plus two is')
> return 2 + 2
> #+end_src
>
> Hit C-c C-c
>
> and got a new block
>
> #+RESULTS:
> : 4
>
>
> I am mystified!
> In python a top level return gives a syntax error.
> Here it works. Is some secret function being created?
>

Yes, when returning a value, the code block itself becomes a function.

>
> Also the results changed to output does not change the behavior

Not true, however you need to put the header arguments on the begin_src
line or on a #+headers line (not on the #+name line).  Also, try to mix
:results output and "return 2 + 2", and you should find the error you
expected earlier.

Best,

#+begin_src python :results value
import time
print("Hello, today's date is %s" % time.ctime())
print('Two plus two is')
return 2 + 2
#+end_src

#+RESULTS:
: 4

#+begin_src python :results output
import time
print("Hello, today's date is %s" % time.ctime())
print('Two plus two is')
#+end_src

#+RESULTS:
: Hello, today's date is Fri Jul  5 08:27:02 2013
: Two plus two is

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

      reply	other threads:[~2013-07-05 14:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-05 12:44 babel - python question Rustom Mody
2013-07-05 14:28 ` Eric Schulte [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vc4pgjy0.fsf@gmail.com \
    --to=schulte.eric@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=rustompmody@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).