简体   繁体   English

类在python的org模式下结果

[英]class results in org mode with python

I am having difficulties in using python classes in org-mode . 我在org-mode中使用python类时遇到困难。
Here is a simple illustration of an org mode file: 这是组织模式文件的简单说明:

First let's define a class 首先让我们定义一个类

#+BEGIN_SRC python :session :exports code
class The_class():
    def __init__(self, a):
        self.a = a

    def add_me(self):
        return self.a + self.a

    def sqr_me(self):
        return self.a**2

#+END_SRC

Then check the class: 然后检查课程:

#+BEGIN_SRC python :session :exports both :results output 
itm = The_class(3)
print('value of itm.a: {0}'.format(itm.a))
print('attributes: {0}'.format(itm.__dict__))
print('methods of itm: {0}'.format(dir(itm)))
#+END_SRC

And make calculation: 并进行计算:

#+BEGIN_SRC python :session :exports both :results output
print(itm.add_me()) 
print(itm.sqr_me())
#+END_SRC

The second block code correctly identifies the attributes, however, it fails to recognize either self.add_me() or self.sqr_me() methods in the dir(self) . 第二个块代码正确地标识了属性,但是,它无法识别dir(self)中的self.add_me()self.sqr_me()方法。 As a consequence, when upon calling itm.add_me() , it gives me a: for example: 结果,当调用itm.add_me() ,它给了我一个:例如:

#+RESULTS: 
: Traceback (most recent call last): 
:   File "<stdin>", line 1, in <module> 
:   File "/var/folders/l7/3vzbfyz93z1fz31m3m6srjcm0000gn/T/babel-18019W4Z/python-1801928I", line 1, in <module> 
:     print(itm.add_me())  
: AttributeError: 'The_class' object has no attribute 'add_me' :

Any ideas what is going on? 有什么想法吗?

The problem comes from the newlines. 问题来自换行符。 Just remove them. 只需删除它们。

#+BEGIN_SRC python :session :exports code
class The_class():
    def __init__(self, a):
        self.a = a
    def add_me(self):
        return self.a + self.a    
    def sqr_me(self):
        return self.a**2

#+END_SRC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM