python-descriptors

    5熱度

    2回答

    在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

    6熱度

    1回答

    我想在返回代理對象的類上創建某種類型的描述符。代理對象在索引時檢索對象的成員並將索引應用於它們。然後它返回總和。 例如, class NDArrayProxy: def __array__(self, dtype=None): retval = self[:] if dtype is not None: return retval.astype(d

    12熱度

    5回答

    我經常發現我需要暫時分配一些成員變量,例如 old_x = c.x old_y = c.y # keep c.z unchanged c.x = new_x c.y = new_y do_something(c) c.x = old_x c.y = old_y ,但我想我可以簡單的寫 with c.x = new_x; c.y = new_y: do_someth

    0熱度

    1回答

    考慮下面的情況下, ITEMLIST類: # filename: item_list.py # locators dictionary is intentionally placed outside `ItemList` class locators = { 'item_id': 'css=tr:nth-of-type({item_num}) .id', 'item_ti

    0熱度

    1回答

    如何編寫描述符,該描述符返回具有描述符並返回其他描述符對象的描述符?在下面的代碼中,getattr中的__get__和__set__以不同實例作爲參數被調用,但它引用了相同的對象。請告知如何通過附加測試。 一般來說,這應該是生成嚴格模式的JSON報告的幫手。它會生成結構,但這些值在結構節點中很常見。只有當我在一個類中有幾個相同類型的類屬性(ObjectField)時纔會暴露這個問題。 class

    4熱度

    1回答

    我想知道是否可以在子類中使用描述符的裝飾器。 class Descriptor(): def __get__(self, instance_obj, objtype): raise Exception('ouch.') def decorate(self, f): print('decorate', f) return f class A

    3熱度

    1回答

    我讀了一些關於你可以用__set__而沒有__get__描述符的事實。 它是如何工作的? 它算作數據描述符嗎?它是一個非數據描述符嗎? 這裏是一個代碼示例: class Desc: def __init__(self, name): self.name = name def __set__(self, inst, value): inst.__dict_

    2熱度

    2回答

    我試圖趕上使用decorator異常的cached_propertyhttps://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76 https://pypi.python.org/pypi/cached-property 我願做如下簡單的東西,但是這並不工作 from pprint import

    0熱度

    1回答

    我知道__slots__做什麼,它應該用於什麼。 但是,我還沒有找到一個全面的答案,如何使用__slots__創建的member描述符的下層機制的作品。 對象級別的值在哪裏實際存儲? 有沒有辦法改變這些值,而不直接屬性訪問描述符? (例如:上課時C有__dict__你可以做C.__dict__['key']代替C.key) 通過創建類似的類級別的描述可以在一個「擴展」 __slots__對象定義的