[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] babel - python question
From: |
Eric Schulte |
Subject: |
Re: [O] babel - python question |
Date: |
Fri, 05 Jul 2013 08:28:23 -0600 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Rustom Mody <address@hidden> 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