我想使用 doctest 注释块来演示特定基类的用法,但是这不能用 doctest 完成,或者我做错了什么。这是我的简单演示代码。
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()
代码不运行。这是发布的第一个错误:
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