在开启了:session的情况下,空行可能会引起错误。 例如
#+BEGIN_SRC python :session "test" :results output
class Test(object):
def m1(self):
pass
def m2(self):
pass
Test().m2()
#+END_SRC
#+RESULTS:
#+begin_example
... ... >>> File "<stdin>", line 1
def m2(self):
^
IndentationError: unexpected indent
File "<stdin>", line 1
pass
^
IndentationError: unexpected indent
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute 'm2'
#+end_example
而下面的写法就没有问题
#+BEGIN_SRC python
class Test(object):
def m1(self):
pass
def m2(self):
pass
Test().m2()
#+END_SRC
#+RESULTS:
: None
至于为什么会这样呢? 看看test buffer中的内容就明白了
class Test(object):
... def m1(self):
... pass
...
>>> def m2(self):
File "<stdin>", line 1
def m2(self):
^
IndentationError: unexpected indent
>>> pass
File "<stdin>", line 1
pass
^
IndentationError: unexpected indent
>>>
>>> Test().m2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Test' object has no attribute 'm2'
>>>
>>>
由于第一个例子中的两个def中存在空行,python解析器误以为类定义在第一个def后就结束了