0
我相信這個問題聽起來有點令人困惑,所以我會在下面給出更多細節。在創建類時動態分配一個動態創建的元類
我有這兩個類定義,其中一個繼承type
:現在
class ProductType(type):
def __new__(cls, name, bases, attrs):
return super(ProductType, cls).__new__(cls, name, bases, attrs)
class Product(object):
__metaclass__ = ProductType
,在運行時,我創建的ProductType
一個子類:
Insurance = type('Insurance', (ProductType,), {})
,然後創建一個子類Product
,將其元類設置爲Insurance
:
HouseInsurance = type('HouseInsurance', (Product,), {'__metaclass__': Insurance})
現在,由於一些明顯的原因(我現在看不出來),如果我做type(HouseInsurance)
我得到ProductType
,而不是Insurance
。看起來動態創建的類由於某種原因忽略了給定的動態創建的元類。爲什麼會發生這種情況,我該如何解決這個問題?
哦,我知道這會很簡單。感謝您指出這一點! – linkyndy