python-descriptors

    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

    3熱度

    1回答

    我正在學習python中的描述符。我想寫一個非數據描述符,但是當我調用classmethod時,具有描述符作爲其類方法的類不會調用空間方法。這是我的例子(沒有__set__): class D(object): "The Descriptor" def __init__(self, x = 1395): self.x = x def __get__(

    1熱度

    2回答

    我在尋找一個優雅(一種有效的)的方式來實現以下Python列表的變化: 我有一個類存儲值的列表作爲字符串(帶分隔,例如。:「 - 」)。我使用一個屬性(getter和setter)這個字符串轉換成一個Python list。 class C(object): def __init__(self, values): """ Construct C with a st

    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__對象定義的