第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Python中創(chuàng)建不可變對象?

如何在Python中創(chuàng)建不可變對象?

慕的地6264312 2019-08-15 16:15:02
如何在Python中創(chuàng)建不可變對象?雖然我從來沒有需要這個,但讓我感到震驚的是在Python中創(chuàng)建一個不可變對象可能會有點棘手。你不能只是覆蓋__setattr__,因為那時你甚至不能設(shè)置屬性__init__。對元組進(jìn)行子類化是一種有效的技巧:class Immutable(tuple):     def __new__(cls, a, b):         return tuple.__new__(cls, (a, b))     @property     def a(self):         return self[0]     @property     def b(self):         return self[1]     def __str__(self):         return "<Immutable {0}, {1}>".format(self.a, self.b)     def __setattr__(self, *ignored):         raise NotImplementedError     def __delattr__(self, *ignored):         raise NotImplementedError但是你可以通過和訪問a和b變量,這很煩人。self[0]self[1]這在純Python中是否可行?如果沒有,我將如何使用C擴(kuò)展?(只能在Python 3中使用的答案是可以接受的)。更新:因此,子類的元組是做純Python,效果很好,除了通過訪問數(shù)據(jù)的另一種可能性的方式[0],[1]等等。所以,要完成這個問題,所有這一切都缺少的是HOWTO在C,做“正確的”,這我懷疑是非常簡單,只是沒有實現(xiàn)任何geititem或setattribute等等。但我不是自己做,我為此提供賞金,因為我很懶。:)
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗 獲得超9個贊

.如何在C中“正確”地做到

您可以使用Cython為Python創(chuàng)建擴(kuò)展類型:

cdef class Immutable:
    cdef readonly object a, b
    cdef object __weakref__ # enable weak referencing support

    def __init__(self, a, b):
        self.a, self.b = a, b

它適用于Python 2.x和3。

測試

# compile on-the-flyimport pyximport; pyximport.install() # $ pip install cythonfrom immutable import Immutableo = Immutable(1, 2)assert o.a == 1, str(o.a)assert o.b == 2try: o.a = 3except AttributeError:
    passelse:
    assert 0, 'attribute must be readonly'try: o[1]except TypeError:
    passelse:
    assert 0, 'indexing must not be supported'try: o.c = 1except AttributeError:
    passelse:
    assert 0, 'no new attributes are allowed'o = Immutable('a', [])assert o.a == 'a'assert o.b == []o.b.append(3) # attribute may contain mutable objectassert o.b == [3]try: o.cexcept AttributeError:
    passelse:
    assert 0, 'no c attribute'o = Immutable(b=3,a=1)assert o.a == 1 and o.b == 3try: del o.bexcept AttributeError:
    passelse:
    assert 0, "can't delete attribute"d = dict(b=3, a=1)o = Immutable(**d)assert o.a == d['a'] and o.b == d['b']o = Immutable(1,b=3)assert o.a == 1 and o.b == 3try: object.__setattr__(o, 'a', 1)except AttributeError:
    passelse:
    assert 0, 'attributes are readonly'try: object.__setattr__(o, 'c', 1)except AttributeError:
    passelse:
    assert 0, 'no new attributes'try: Immutable(1,c=3)except TypeError:
    passelse:
    assert 0, 'accept only a,b keywords'for kwd in [dict(a=1), dict(b=2)]:
    try: Immutable(**kwd)
    except TypeError:
        pass
    else:
        assert 0, 'Immutable requires exactly 2 arguments'

如果您不介意索引支持,那么@Sven Marnachcollections.namedtuple建議您更喜歡:

Immutable = collections.namedtuple("Immutable", "a b")


查看完整回答
反對 回復(fù) 2019-08-15
  • 3 回答
  • 0 關(guān)注
  • 714 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號