emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] odd/unexpected behavior with Python src blocks


From: John Kitchin
Subject: [O] odd/unexpected behavior with Python src blocks
Date: Sun, 28 Sep 2014 18:11:46 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4.50 (darwin)

* Odd behavior in python src-blocks in org-mode

I found that some code blocks in Python execute due to apparently silent
errors! here is how to reproduce it with a vanilla emacs -q.

#+BEGIN_SRC emacs-lisp
(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (python . t)))

(setq org-confirm-babel-evaluate nil)
#+END_SRC

#+RESULTS:

This block should raise an error (and does) because k is not defined.
#+BEGIN_SRC python
def f(y, x):
    return k * y

print(f(1, 0))
#+END_SRC

#+RESULTS:

It raises this error.

#+BEGIN_EXAMPLE
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "<stdin>", line 2, in f
NameError: global name 'k' is not defined
#+END_EXAMPLE

However, this code block actually executes, and gives the wrong answer! If I 
open the source block in Python mode, it does not run without error.

#+BEGIN_SRC python :results output
from scipy.integrate import odeint

def f(y, x):
    return k * y

print(odeint(f, 1, [0, 1]))
#+END_SRC

#+RESULTS:
: [[ 1.]
:  [ 1.]]

Here is the correct answer.

#+BEGIN_SRC python :results output
from scipy.integrate import odeint

k = -1
def f(y, x):
    return k * y

print(odeint(f, 1, [0, 1]))
#+END_SRC

#+RESULTS:
: [[ 1.        ]
:  [ 0.36787947]]

I am not sure why this happens, but it seems like incorrect behavior to
me.

-- 
-----------------------------------
John Kitchin
http://kitchingroup.cheme.cmu.edu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]