溫馨提示×

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

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

ellipsis如何在Python項(xiàng)目中使用

發(fā)布時(shí)間:2020-11-21 14:55:05 來(lái)源:億速云 閱讀:192 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)ellipsis如何在Python項(xiàng)目中使用 ,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

背景

在 Python 的基本類(lèi)型中單例模式的值有三個(gè) None 類(lèi)型的 None ,NotImplemented 類(lèi)型的 NotImplemented, Ellipsis 類(lèi)型的 ... 。

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è)我們編寫(xiě)了一個(gè)類(lèi),要想知道這個(gè)有沒(méi)有語(yǔ)法層面的錯(cuò)誤,只要簡(jiǎn)單的調(diào)用一下就能測(cè)試出來(lái)。為了把這個(gè)測(cè)試自動(dòng)化,于是做成了文檔測(cè)試。

#!/usr/bin/evn python3

class Person(object):
  """人類(lèi)類(lèi)型
  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è),而我們的用例上寫(xiě)死了。誠(chéng)然這個(gè)問(wèn)題用 unittest 可以解決,但是這個(gè)不是這里要講的。

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 這個(gè)指令,改造后的代碼如下。

#!/usr/bin/evn python3


class Person(object):
  """人類(lèi)類(lèi)型
  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 -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.

其它

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

#!/usr/bin/evn python3


class Person(object):
  """人類(lèi)類(lèi)型
  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()

看完上述內(nèi)容,你們對(duì)ellipsis如何在Python項(xiàng)目中使用 有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

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

AI