本文介绍了Python:如何在doctest中定义一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用doctest注释块来演示特定基类的用法,但是使用doctest无法做到这一点,或者我做错了什么.这是我简单的演示代码.
I would like to use a doctest comment block to demonstrate the usage of a particular base class, but either this cannot be done with doctest or I am doing something wrong.Here is my simple demo code.
class MyClass(object):
'''
>>> m = MyClass()
>>> print m.x
1
>>> class A(MyClass):
>>> def __init__(self):
>>> super(A,self).__init__()
>>>
>>> a = A()
>>> print a.x
1
'''
def __init__(self):
self.x = 1
if __name__ == "__main__":
import doctest
doctest.testmod()
代码未运行.这是发出的第一个错误:
The code doesn't run. Here's the first error issued:
Failed example:
class A(MyClass):
Exception raised:
Traceback (most recent call last):
File "C:\Python27\lib\doctest.py", line 1254, in __run
compileflags, 1) in test.globs
File "<doctest __main__.MyClass[2]>", line 1
class A(MyClass):
^
SyntaxError: unexpected EOF while parsing
推荐答案
在解释器中尝试一下;它使用 ...
来显示连续行.>>>>
仅适用于 new 语句或表达式,而 class
则不完整,直到您有空的 ...
续行:
Try it out in the interpreter; it uses ...
to show continuation lines. >>>
is only for a new statement or expression, while a class
in incomplete until you've had an empty ...
continuation line:
>>> class A(MyClass):
... def __init__(self):
... super(A, self).__init__()
...
这篇关于Python:如何在doctest中定义一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!