python3 文档测试

在python交互命令行里面,最后加上  -v  会显示的更加全面

python3 test3.py -v

class Dict(dict):
    '''
    Simple dict but also support access as x.y style.

    >>> d1 = Dict()
    >>> d1['x'] = 100
    >>> d1.x
    100
    >>> d1.y = 200
    >>> d1['y']
    200
    >>> d2 = Dict(a=1, b=2, c='3')
    >>> d2.c
    '3'
    >>> d2['empty']
    Traceback (most recent call last):
        ...
    KeyError: 'empty'
    >>> d2.empty
    Traceback (most recent call last):
        ...
    AttributeError: 'Dict' object has no attribute 'empty'
    '''
    def __init__(self, **kw):
        super(Dict, self).__init__(**kw)

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'Dict' object has no attribute '%s'" % key)

    def __setattr__(self, key, value):
        self[key] = value

if __name__=='__main__':
    import doctest
    doctest.testmod()

显示结果

wulimindeMacBook-Pro:Python wulimin$ python3 test3.py -v
Trying:
    d1 = Dict()
Expecting nothing
ok
Trying:
    d1['x'] = 100
Expecting nothing
ok
Trying:
    d1.x
Expecting:
    100
ok
Trying:
    d1.y = 200
Expecting nothing
ok
Trying:
    d1['y']
Expecting:
    200
ok
Trying:
    d2 = Dict(a=1, b=2, c='3')
Expecting nothing
ok
Trying:
    d2.c
Expecting:
    '3'
ok
Trying:
    d2['empty']
Expecting:
    Traceback (most recent call last):
        ...
    KeyError: 'empty'
ok
Trying:
    d2.empty
Expecting:
    Traceback (most recent call last):
        ...
    AttributeError: 'Dict' object has no attribute 'empty'
ok
4 items had no tests:
    __main__
    __main__.Dict.__getattr__
    __main__.Dict.__init__
    __main__.Dict.__setattr__
1 items passed all tests:
   9 tests in __main__.Dict
9 tests in 5 items.
9 passed and 0 failed.
Test passed.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值