![](/img/trans.png)
[英]Python typing for a metaclass Singleton
我有一個 singleton 的 Python (3.8) 元類,如此處所示我試圖像這樣添加類型:from typing import Dict, Any, TypeVar, Type _T = TypeVar("_T", bound="Singleton") class Singleton ...
[英]Python typing for a metaclass Singleton
我有一個 singleton 的 Python (3.8) 元類,如此處所示我試圖像這樣添加類型:from typing import Dict, Any, TypeVar, Type _T = TypeVar("_T", bound="Singleton") class Singleton ...
[英]Error implementing singleton in Python with a metaclass
我想用 python 元類實現 singleton。 我創建了兩個線程,實例化“文件”class 並驗證兩者的哈希碼是否相同。 最后我打印了 class 的“_file”屬性。 這些對象是為我打印的,但我看到一個錯誤,我不知道是什么原因造成的錯誤如下: 我的代碼如下: ...
[英]generic Class in Python
我想用一種方法在 Python 中做一個泛型類此類不生成其實例一些屬性設置在類的主體中此類的方法使用設置的屬性,在生成的類中,此方法的輸出取決於這些屬性方法只有一個輸入我知道沒有元類是行不通的,但我不知道如何應用它們:) 是這樣的: ...
[英]Why does defining new class sometimes call the __init__() function of objects that the class inherits from?
我試圖了解當您聲明一個新的 class 時實際發生了什么,它繼承自 python 中的父 class。 這是一個非常簡單的代碼片段: 如果我運行它,則沒有錯誤,並且 output 與我預期的一樣。 這是一個帶有明顯錯誤的腳本,我繼承自Foo()的實例而不是 class Foo 這段代碼運行時沒有崩 ...
[英]Which class was written first in Python: type or object?
所有類都是object的子類,同時它們都是type的實例。 因此, object是type的實例, type是object的子類。 但是,顯然,這些聯系之一是虛構的。 python的實現中先寫哪個class? ...
[英]Is there a way to implement a __post_init__ method for inherited classes?
我想防止實例的用戶在使用所述 object 時錯誤地創建不存在的屬性。 假設我有一個帶有 init 和一些屬性的 class: 我想啟用設置現有屬性,同時防止創建新屬性: 使用 __setattr__ 覆蓋和__setattr__相當容易: 現在我掙扎的步驟是當一個新的 class 繼承 Foo ...
[英]Extending Generic.__class_getitem__ in Python to accept more params
如何為 Python Generic class 擴展__class_getitem__ ? 我想將 arguments 添加到__class_getitem__ ,同時將一些向上傳播到Generic.__class_getitem__ 。 請參閱下面的代碼片段以獲取示例用例(不運行):from t ...
[英]Python How to call child class method from Parent metaclass
我正在嘗試編寫自己的測試運行器實現,我遇到的是某種 setUp 和 TearDown 方法以及如何覆蓋/調用它們。 當前的 output 是: 但是預期的 output 將使用來自子類 Class 的函數覆蓋元類中的這些函數,如下所示: 但我真的不太了解元類以及如何將子方法傳遞給父 class。 ...
[英]Can I implement a class method dynamically in Python?
在 Python 中,可以動態實現 class 實例的屬性已不是什么秘密。 看起來是這樣的: 結果: 有沒有辦法用 class 屬性而不是 class 實例屬性來做同樣的事情? 這是一個例子: 我希望這行代碼返回一個在我的代碼中某處計算的值,而不必在 MyClass class 上明確定義“dyn ...
[英]Weird behavior when using Singleton metaclass in Python unittest TestCase mixin
背景我們有一個特殊的設置,我想為此編寫一個 TestCase 基礎 class / mixin,可用於為每個 TestCase 提供開箱即用的功能。 為 TestCase 提供功能的 class 需要是單個實例,因此實現為 singleton。 觀察制作 singleton 時, 測試發現不再正常工 ...
[英]Calling the default code for metaclass's __init__
所以我有super(MyMeta, cls).__new__(cls, name, bases, attr)來調用默認的__new__ function? 我可以為__init__做類似的事情嗎? 為元類調用默認的__init__ function。 ...
[英]how can we write a metaclass which returns the original object un-touched instead of calling the copy constructor?
我傾向於編寫許多將輸入 arguments 轉換為用戶定義的 class 實例的函數。class Klaus: def __init__(*args): pass def fnctn(arg:Klaus): arg = Klaus(arg) print(a ...
[英]how do I create a meta-class such that instances of the class are wrapped in `functools.wraps`?
我想用定義了__call__方法的 object 包裝一個 function 名稱funky_the_function 。 不使用functools.wraps包裝的 function 的名稱將丟失,文檔字符串也將丟失。 如何創建一個元類,以便將 class 的實例包裝在functools.wra ...
[英]Making classfactory that derive from dataclass objects informed by types in python
使用dataclasses.dataclass時,類型信息會告知如何解析參數。 我想采用一個已定義的數據類並生成一個dataclass ,它將所有屬性類型聲明從X更改為Optional[List[X]] 。from dataclasses import dataclass from dataclas ...
[英]Are there any unique features provided only by metaclasses in Python?
我已閱讀此問題的答案: Python 中的元類是什么? 這個問題: 在 Python 中,我什么時候應該使用元 class? 並瀏覽文檔:數據 model 。 我很可能錯過了一些東西,我想澄清一下:元類是否可以在其他工具(裝飾器、inheritance 等)的幫助下正確或不正確地完成任何事情(un ...
[英]See if type meta object is an instance of another class
我想知道是否有一種方法可以查看 class 是否繼承自另一個 class 基於給定 object 的type 假設我有如下定義的 class MyList :class MyList(list): pass 現在考慮以下幾點:>>> my_list = MyList() ...
[英]python metaclass conflict : how to pass additional parameter to meta class apart from the parent class
這是我嘗試創建自定義異常 class 的代碼片段,它采用有關異常的輸入參數 我打算如下所述使用它 我收到此錯誤: 我不確定是否可以這樣做。 如果是的話誰能指導我謝謝 ...
[英]How Meta classes works in python?
問題的起源我最近使用 django 並習慣了模型、序列化器和表單中的 Meta 類。 到目前為止,我的理解是我知道元類用於創建類。 當定義一個類時,Python 將遍歷類內部並收集所有屬性和方法並存儲為字典,然后搜索__metaclass__屬性,如果已定義,它將使用該類創建已定義的類,否則將使用默 ...
[英]Inherit decorators with multiple decorators
我使用以下 Mixin 類沿子類繼承裝飾器。 問題是當一個方法有多個裝飾器時,它們不會被識別(只是最后一個)。 例如,如果我有課程: 然后任何子類都只會繼承 decorator1 而不是我想要的 decorator2。 這是 Mixin 類: 謝謝你的幫助。 對於任何裝飾師,我都會這樣做: ...
[英]Using Python Metaclasses to Limit the Number of Attributes
我試圖定義一個 Python 元類來限制一個類可能包含的屬性數量,從創建時開始。 由於系統限制,我只能使用 Python 3.7 我設法在沒有元類的情況下做到了: class MyClass: def __init__(self): self.param1 = 7 ...