Python ellipsis 的用法詳解
背景
在 Python 的基本類型中單例模式的值有三個(gè) None 類型的 None ,NotImplemented 類型的 NotImplemented, Ellipsis 類型的 ... 。
None 已經(jīng)用的爛大街了,NotImplemented 也比較常用,唯獨(dú) ... 在江湖上只知它是三巨頭之一,但不知其用法。
Ellipsis
Ellipsis 在 python 中代表“省略”,用現(xiàn)在的流形語(yǔ)來(lái)表達(dá)就是“老鐵,不要在意這些細(xì)節(jié)!”。哪什么時(shí)候要告訴別人不要在意這些細(xì)節(jié)呢?其中的一個(gè)場(chǎng)景就是隨機(jī)值。
用于文檔測(cè)試
假設(shè)我們編寫了一個(gè)類,要想知道這個(gè)有沒(méi)有語(yǔ)法層面的錯(cuò)誤,只要簡(jiǎn)單的調(diào)用一下就能測(cè)試出來(lái)。為了把這個(gè)測(cè)試自動(dòng)化,于是做成了文檔測(cè)試。
#!/usr/bin/evn python3class Person(object): '''人類類型 Parameters: ---------- name: str age: int Return: ------ >>> Person() <main.Person object at 0x7ff36c1ca250> ''' name = ’’ age = 0 def __init__(self, name: str = ’tom’, age: int = 10) -> ’Person’: '''初始化 ''' self.name = name self.age = age def say_hello(self) -> str: '''返回打招呼信息 ''' return f'Hello My name is {self.name} .'
當(dāng)我們運(yùn)行測(cè)試用例時(shí)會(huì)報(bào)錯(cuò),原因是每次創(chuàng)建的對(duì)象,它的內(nèi)存地址并不等于測(cè)試用例中指定的哪個(gè),而我們的用例上寫死了。誠(chéng)然這個(gè)問(wèn)題用 unittest 可以解決,但是這個(gè)不是這里要講的。
python3 -m doctest main.py -vTrying: Person()Expecting: <main.Person object at 0x7ff36c1ca250>**********************************************************************File '/private/tmp/main.py', line 12, in main.PersonFailed example: Person()Expected: <main.Person object at 0x7ff36c1ca250>Got: <main.Person object at 0x7fe4e078ac70>3 items had no tests: main main.Person.__init__ main.Person.say_hello**********************************************************************1 items had failures: 1 of 1 in main.Person1 tests in 4 items.0 passed and 1 failed.***Test Failed*** 1 failures.
哪如何才能告訴 doctest 這位老鐵不要在意返回值細(xì)節(jié)呢?答案是加上 Ellipsis 這個(gè)指令,改造后的代碼如下。
#!/usr/bin/evn python3class Person(object): '''人類類型 Parameters: ---------- name: str age: int Return: ------ >>> Person() #doctest: +ELLIPSIS <main.Person object at 0x...> ''' name = ’’ age = 0 def __init__(self, name: str = ’tom’, age: int = 10) -> ’Person’: '''初始化 ''' self.name = name self.age = age def say_hello(self) -> str: '''返回打招呼信息 ''' return f'Hello My name is {self.name} .'
運(yùn)行測(cè)試用例這下可以通過(guò)了。
python3 -m doctest main.py -vTrying: Person() #doctest: +ELLIPSISExpecting: <main.Person object at 0x...>ok3 items had no tests: main main.Person.__init__ main.Person.say_hello1 items passed all tests: 1 tests in main.Person1 tests in 4 items.1 passed and 0 failed.Test passed.
其它
如果我們是為模塊添加測(cè)試用例,那么可以這樣做,會(huì)方便一些。
#!/usr/bin/evn python3class Person(object): '''人類類型 Parameters: ---------- name: str age: int Return ------ >>> Person() #doctest: +ELLIPSIS <...Person object at 0x...> ''' name = ’’ age = 0 def __init__(self, name: str = ’tom’, age: int = 10) -> ’Person’: '''初始化 ''' self.name = name self.age = age def say_hello(self) -> str: '''返回打招呼信息 ''' return f'Hello My name is {self.name} .'if __name__ == '__main__': # 因?yàn)樵谀K在被 import 的時(shí)候 __name__ 直接等于 模塊名 不等于 “__main__” ,所以在作為模塊被導(dǎo)入時(shí)并不會(huì)執(zhí)行測(cè)試用例 # 如果想執(zhí)行測(cè)試用例直接執(zhí)行模塊就行 import doctest doctest.testmod()
以上就是Python ellipsis 的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于Python ellipsis的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效2. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介3. asp批量添加修改刪除操作示例代碼4. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法5. 刪除docker里建立容器的操作方法6. asp在iis7報(bào)錯(cuò)行號(hào)不準(zhǔn)問(wèn)題的解決方法7. 推薦一個(gè)好看Table表格的css樣式代碼詳解8. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……9. asp讀取xml文件和記數(shù)10. 概述IE和SQL2k開(kāi)發(fā)一個(gè)XML聊天程序
