久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

修補(bǔ)函數(shù)的 __call__

Patch __call__ of a function(修補(bǔ)函數(shù)的 __call__)
本文介紹了修補(bǔ)函數(shù)的 __call__的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我需要在測(cè)試中修補(bǔ)當(dāng)前日期時(shí)間.我正在使用這個(gè)解決方案:

I need to patch current datetime in tests. I am using this solution:

def _utcnow():
    return datetime.datetime.utcnow()


def utcnow():
    """A proxy which can be patched in tests.
    """
    # another level of indirection, because some modules import utcnow
    return _utcnow()

然后在我的測(cè)試中,我會(huì)執(zhí)行以下操作:

Then in my tests I do something like:

    with mock.patch('***.utils._utcnow', return_value=***):
        ...

但今天我想到了一個(gè)想法,我可以通過修補(bǔ)函數(shù) utcnow__call__ 來簡化實(shí)現(xiàn),而不是額外添加一個(gè) _utcnow.

But today an idea came to me, that I could make the implementation simpler by patching __call__ of function utcnow instead of having an additional _utcnow.

這對(duì)我不起作用:

    from ***.utils import utcnow
    with mock.patch.object(utcnow, '__call__', return_value=***):
        ...

如何優(yōu)雅地做到這一點(diǎn)?

How to do this elegantly?

推薦答案

當(dāng)你修補(bǔ)一個(gè)函數(shù)的 __call__ 時(shí),你是在設(shè)置那個(gè) __call__ 屬性實(shí)例.Python 實(shí)際上調(diào)用了類上定義的 __call__ 方法.

When you patch __call__ of a function, you are setting the __call__ attribute of that instance. Python actually calls the __call__ method defined on the class.

例如:

>>> class A(object):
...     def __call__(self):
...         print 'a'
...
>>> a = A()
>>> a()
a
>>> def b(): print 'b'
...
>>> b()
b
>>> a.__call__ = b
>>> a()
a
>>> a.__call__ = b.__call__
>>> a()
a

將任何東西分配給 a.__call__ 是沒有意義的.

Assigning anything to a.__call__ is pointless.

但是:

>>> A.__call__ = b.__call__
>>> a()
b

TLDR;

a() 不調(diào)用 a.__call__.它調(diào)用 type(a).__call__(a).

TLDR;

a() does not call a.__call__. It calls type(a).__call__(a).

回答為什么type(x).__enter__(x) 而不是 Python 標(biāo)準(zhǔn) contextlib 中的 x.__enter__()?".

There is a good explanation of why that happens in answer to "Why type(x).__enter__(x) instead of x.__enter__() in Python standard contextlib?".

特殊方法查找的 Python 文檔中記錄了此行為.

這篇關(guān)于修補(bǔ)函數(shù)的 __call__的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?)
How to call self in a mock method of an object in Python?(如何在 Python 中對(duì)象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對(duì)象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
How to properly use mock in python with unittest setUp(如何通過 unittest setUp 在 python 中正確使用 mock)
主站蜘蛛池模板: 黄色国产片 | 国产理论在线 | 91理论片午午伦夜理片久久 | 日韩视频免费看 | 国产一区二区影院 | 色婷婷国产 | 中文在线免费观看 | 日韩3级| 精品免费观看 | 欧美日韩一区二区三区四区 | 日本不卡视频 | 在线观看日韩av | 97久久精品人人澡人人爽 | 天天操夜夜爽 | av福利在线| 日韩欧美高清 | 国产涩涩 | 国产51自产区 | 麻豆av在线免费观看 | 国产天天操 | 日韩av一级片| 国产成人三级 | 另类一区二区 | 看逼网站 | 国产精品99久久久久久www | 成人国产精品一区二区 | 黄色网免费 | 午夜精品影院 | 成年人免费看片 | 欧美日韩激情 | 久久精品人人 | 日本a在线观看 | av日韩在线播放 | 久久国产精品免费 | 国产视频在线观看视频 | 色综合久久88色综合天天 | 婷婷99 | av在线免费网站 | 中文字幕色哟哟 | 国产精品一区二 | 成人精品三级av在线看 |