欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python ellipsis 的用法詳解

 更新時間:2020年11月20日 15:55:16   作者:蔣樂興  
這篇文章主要介紹了Python ellipsis 的用法詳解,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

背景

在 Python 的基本類型中單例模式的值有三個 None 類型的 None ,NotImplemented 類型的 NotImplemented, Ellipsis 類型的 ... 。

None 已經(jīng)用的爛大街了,NotImplemented 也比較常用,唯獨 ... 在江湖上只知它是三巨頭之一,但不知其用法。

Ellipsis

Ellipsis 在 python 中代表“省略”,用現(xiàn)在的流形語來表達(dá)就是“老鐵,不要在意這些細(xì)節(jié)!”。哪什么時候要告訴別人不要在意這些細(xì)節(jié)呢?其中的一個場景就是隨機(jī)值。

用于文檔測試

假設(shè)我們編寫了一個類,要想知道這個有沒有語法層面的錯誤,只要簡單的調(diào)用一下就能測試出來。為了把這個測試自動化,于是做成了文檔測試。

#!/usr/bin/evn python3

class 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)我們運行測試用例時會報錯,原因是每次創(chuàng)建的對象,它的內(nèi)存地址并不等于測試用例中指定的哪個,而我們的用例上寫死了。誠然這個問題用 unittest 可以解決,但是這個不是這里要講的。

python3 -m doctest main.py -v
Trying:
  Person()
Expecting:
  <main.Person object at 0x7ff36c1ca250>
**********************************************************************
File "/private/tmp/main.py", line 12, in main.Person
Failed 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.Person
1 tests in 4 items.
0 passed and 1 failed.
***Test Failed*** 1 failures.

哪如何才能告訴 doctest 這位老鐵不要在意返回值細(xì)節(jié)呢?答案是加上 Ellipsis 這個指令,改造后的代碼如下。

#!/usr/bin/evn python3


class 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} ."

運行測試用例這下可以通過了。

python3 -m doctest main.py -v
Trying:
  Person() #doctest: +ELLIPSIS
Expecting:
  <main.Person object at 0x...>
ok
3 items had no tests:
  main
  main.Person.__init__
  main.Person.say_hello
1 items passed all tests:
  1 tests in main.Person
1 tests in 4 items.
1 passed and 0 failed.
Test passed.

其它

如果我們是為模塊添加測試用例,那么可以這樣做,會方便一些。

#!/usr/bin/evn python3


class 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__":
  # 因為在模塊在被 import 的時候 __name__ 直接等于 模塊名 不等于 “__main__” ,所以在作為模塊被導(dǎo)入時并不會執(zhí)行測試用例
  # 如果想執(zhí)行測試用例直接執(zhí)行模塊就行
  import doctest
  doctest.testmod()

以上就是Python ellipsis 的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于Python ellipsis的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論