溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python 文檔測試:doctest

發(fā)布時間:2020-07-17 08:56:07 來源:網(wǎng)絡(luò) 閱讀:441 作者:虎皮喵的喵 欄目:編程語言

doctest作用:會把文檔中注釋的代碼提取并進行測試。


#!/usr/bin/python
# -*- coding: utf-8 -*-

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()

什么也沒有輸出,證明程序正確。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI