在Python 3 class A(object):
attr = SomeDescriptor()
...
def somewhere(self):
# need to check is type of self.attr is SomeDescriptor()
desc = self.__class__.__dict__[attr_name
我想在返回代理對象的類上創建某種類型的描述符。代理對象在索引時檢索對象的成員並將索引應用於它們。然後它返回總和。 例如, class NDArrayProxy:
def __array__(self, dtype=None):
retval = self[:]
if dtype is not None:
return retval.astype(d
如何編寫描述符,該描述符返回具有描述符並返回其他描述符對象的描述符?在下面的代碼中,getattr中的__get__和__set__以不同實例作爲參數被調用,但它引用了相同的對象。請告知如何通過附加測試。 一般來說,這應該是生成嚴格模式的JSON報告的幫手。它會生成結構,但這些值在結構節點中很常見。只有當我在一個類中有幾個相同類型的類屬性(ObjectField)時纔會暴露這個問題。 class
我想知道是否可以在子類中使用描述符的裝飾器。 class Descriptor():
def __get__(self, instance_obj, objtype):
raise Exception('ouch.')
def decorate(self, f):
print('decorate', f)
return f
class A