![](/img/trans.png)
[英]How to add additional arguments to existing decorator with custom decorator
我知道我的問題聽起來有點傻,但是圖書館里有一個現有的裝飾器,它可以接受也可以不接受 kwarg。 我想在達到特定條件時傳遞此參數。 所以我最初的想法是寫一個裝飾器來包裝這個 function。但我不知道這在 Python 中是否可行。 例如,這是原始裝飾器: 如果某個條件被調用,則需要像這樣調用它 ...
[英]How to add additional arguments to existing decorator with custom decorator
我知道我的問題聽起來有點傻,但是圖書館里有一個現有的裝飾器,它可以接受也可以不接受 kwarg。 我想在達到特定條件時傳遞此參數。 所以我最初的想法是寫一個裝飾器來包裝這個 function。但我不知道這在 Python 中是否可行。 例如,這是原始裝飾器: 如果某個條件被調用,則需要像這樣調用它 ...
[英]Decorating with arguments a Class Declaration
我正在嘗試創建一個 Class 表示要為我的應用程序執行的正則表達式。 對於每個正則表達式,我都有一個指向 regex101.com 頁面的鏈接,用戶可以在其中找到單元測試。 我想使用這個解決方案在 class 聲明附近有這個鏈接,但沒有在 class 代碼中。 我想到的代碼必須如下所示:class ...
[英]Reuse function as pytest fixture
我的代碼中有一個 function,fastapi 使用它向端點提供一個 db session: 我想使用與 pytest 夾具相同的 function。 如果我執行以下操作,則無法識別燈具: test_item_create拋出一個關於db not being a fixture: fixtur ...
[英]decorating method using instance method
我正在嘗試用另一個實例方法裝飾來裝飾我的實例 car_details 但我無法弄清楚為什么會出現錯誤。 當我像下面這樣調用 call car_details 方法時 然后它可以正常工作,但是當我當時用@decorator 嘗試它時,我遇到了問題。 類型錯誤:decor() 缺少 1 個必需的位置參數 ...
[英]Decorator's return is different from the actual return
代碼為什么我得到不同的結果?from time import time def speed_test(func): def wrapper(*args, **kwargs): start = time() func(*args, **kwargs) ...
[英]Using decorators that are aware of `self`'s state
簡而言之,我有一個 DataFormatter class 有兩種可能的狀態: train或infer ,它應該類似於許多具有fit和transform功能的 sklearn 庫:如果模式是train我想在self.metadata中存儲一個列表已進行的 function 調用和參數,以便可以簡單 ...
[英]Decorator/package to save all intermediate variables in a function, should the function fail
我發現自己經常遇到這類問題。 我有一個 function 之類的def compute(input): result = two_hour_computation(input) result = post_processing(result) return result 和 ...
[英]How to type hint a function, added to class by class decorator in Python
我有一個 class 裝飾器,它為裝飾的 class 添加了一些函數和字段。@mydecorator @dataclass class A: a: str = "" 添加(通過setattr() )是一個.save() function 和一組數據類字段的信息作為單獨的字典。 我希望 V ...
[英]How to add decorator to dynamically create class
我想將此代碼轉換為動態代碼: 我可以通過這種方式動態創建 class: 但是我不知道如何添加裝飾器@external_decorator 。 謝謝 ...
[英]Defered invocation of a python classmethod obtained in a decorator
我有一個裝飾器用來包裝這樣的類方法:class Class(object): @register_classmethod @classmethod def my_class_method(cls): ... 我的裝飾器得到一個classmethod當我 ...
[英]How to have typing support for a static property (using a decorator)
給定一個 static 屬性裝飾器:class static_property: def __init__(self, getter): self.__getter = getter def __get__(self, obj, objtype): ...
[英]Saving the arguments of a function each time it is called, using a decorator defined in a parent class
我有一系列的父子類 A(),B(A),......可能在不同的模塊中。 用戶將導入其中一個類並定義自己的子類 class Z(X) 派生自 X = A, B,...。然后他將使用在父類 class X 中定義的初始化方法初始化他的實例,或者在他在 Z 中進行了重寫初始化。我希望初始化 functio ...
[英]How to typecheck a property with a class decorator
我正在努力讓這段代碼正確地使用mypy進行類型檢查: 這給了我這個錯誤,即使它正確運行打印42 (注意我在運行reveal_type時取消了 reveal_type 的mypy ): 此代碼正確地使用mypy進行類型檢查: 但這顯然不能正確運行: 請注意,如果我使用 function 裝飾器而不是使 ...
[英]How to pass self into a decorated method?
我正在嘗試使用裝飾器將隱私修改器實現到 python 中。 我的問題是,每當我裝飾一個以 self 作為參數的方法時,當使用點表示法調用該方法時,它不會自動傳遞 self 。 公共裝潢師class:class Public: def __init__(self, method, *args ...
[英]How to type annotate a multi-level decorator
我正在嘗試注釋一個注入器裝飾器,它在調用 function 時將全局字典中的值作為關鍵字參數注入到裝飾的 function 中。 任何有使用參數注釋裝飾器經驗的人都可以幫助我嗎? 嘗試注釋但陷入以下錯誤:import functools import inspect from typing impo ...
[英]Declaring a function parameter inside a decorator
我需要在裝飾器內部創建參數,以免在多個方法中重復代碼,但是,我遇到了一個錯誤,而且我不確定這種使用命名空間的方式是如何正確的。 我想知道這種結構在程序中的可接受程度如何,是否值得使用它? 我的嘗試是這樣的: | NameError: 名稱 'config' 未定義此外,如果我們直接放置 func ...
[英]How to inject arguments from a custom decorator to a command in discord.py?
我正在開發一個機器人,它可以跟蹤不同渠道中的各種基於文本的游戲。 在運行相關游戲的通道之外使用的命令當然應該什么也不做,並且它們也不應該在游戲未運行時激活(例如,當新游戲即將開始時)。 因此,幾乎我所有的命令都以相同的幾行代碼開頭 我寧願只裝飾所有這些方法。 Discord.py 方便地提供了一 ...
[英]Track invocations of methods and functions
我正在尋找允許跟蹤方法和函數調用的庫。 將其視為提供called和call_count屬性的Mock 。 所需最終結果示例:s = MagicProxyLib() @s class MyClass: def not_called(self): print("This is ...
[英]How can I implement LRU cache in my maya scene using python?
我正在嘗試在我的 python 程序中實現 LRU 緩存,我想將 object 名稱及其存在於我的 Maya 場景中的類型存儲到 LRU 緩存中,然后我想創建兩個將返回名稱和對象類型的函數。 ...
[英]Why is my decorator not working when using the alternative to @ syntax?
我正在為我的命令行程序制作一個工具,我不會在這個問題上爭論它的使用。 事實上,我使用裝飾器將功能分配給“令牌”。 當使用 @decorator 語法時,我的代碼可以完美地工作,但是當我使用替代語法時卻無法執行任何操作(請原諒我,因為我不知道名字)。 下面是裝飾器的相關函數:def __add_co ...