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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將對(duì)象 A 的實(shí)例的數(shù)據(jù)綁定到對(duì)象 B 的對(duì)應(yīng)實(shí)例?

如何將對(duì)象 A 的實(shí)例的數(shù)據(jù)綁定到對(duì)象 B 的對(duì)應(yīng)實(shí)例?

浮云間 2021-12-17 10:26:07
我正在編寫另一個(gè)“自行車”來讀取 .ini 文件,以獲得一些高級(jí) Python 和 OOP 的經(jīng)驗(yàn)。此外,我發(fā)現(xiàn)默認(rèn)的 Python 的 configparser 訪問風(fēng)格對(duì)眼睛來說有點(diǎn)不舒服,即:something = config['section']['parameter']而不是更多的 eye-candish something = config.section.parameter。我已經(jīng)結(jié)束了這個(gè)設(shè)計(jì):ConfigStorage 對(duì)象將參數(shù)/值對(duì)保留為字典;Config 對(duì)象來管理存儲(chǔ)對(duì)象中的數(shù)據(jù)。__getattribute__in Configobject 被覆蓋,因此我可以ConfigStorage通過Config對(duì)象的屬性名稱訪問字典中的值(目標(biāo)?。.?dāng)Config對(duì)象只有一個(gè)實(shí)例時(shí),系統(tǒng)工作正常。當(dāng)我嘗試使用多個(gè)配置(并因此實(shí)例化新Config對(duì)象來管理它們)時(shí),我意識(shí)到這ConfigStorage是一個(gè)通用的.我試圖ConfigStorage為每個(gè)Config對(duì)象創(chuàng)建新實(shí)例,但 Python 在這段代碼中達(dá)到了最大遞歸深度(為什么?):class _ConfigStorage(object):    def __init__(self):        self.config = dict()class Config(object):    def __getattribute__(self, name):        if name in self.c.config:            return self.c.config[name]        else:            raise AttributeError("No parameter named [%s]" % name)    def __init__(self, file):        self.c = _ConfigStorage()        with open(file, 'r') as f:            for config_line in f:                key = # getting key from line                value = # getting value                self.c.config[key] = value最后,我讓我的代碼工作了,但我發(fā)現(xiàn)這個(gè)解決方案非常粗糙并且被黑魔法污染了。我通過向字典鍵添加Config對(duì)象 ID的字符串表示來分離存儲(chǔ)對(duì)象中的數(shù)據(jù)。該ConfigStorage對(duì)象仍是單身。還__del__需要方法,因?yàn)?GC 不知道存儲(chǔ)中的不必要數(shù)據(jù),我必須手動(dòng)刪除它們。糟透了。class _ConfigStorage(object):    config = dict()class Config(object):    def __getattribute__(self, name):        key = str(id(self)) + name        if key in _ConfigStorage.config:            return _ConfigStorage.config[key]        else:            raise AttributeError("No parameter named [%s]" % name)    def __init__(self, file):        with open(file, 'r') as f:            for config_line in f:                key = str(id(self) + # getting key from line                value = # getting value                _ConfigStorage.config[key] = value如何改進(jìn)設(shè)計(jì),能夠通過對(duì)象屬性訪問字典數(shù)據(jù)?
查看完整描述

2 回答

?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

我認(rèn)為此實(shí)現(xiàn)適合您的需求:


class Config(object):

    def __getattr__(self, name):

        if name in self.config:

            return self.config[name]

        else:

            raise AttributeError("No parameter named [%s]" % name)


    def __init__(self, file=None):

        # changed to make it work without real file

        self.config = {'key': 'value'}


config = Config()

print(config.key)  # value

print(config.not_a_key)  # AttributeError: ...

不需要寫_ConfigStorage和__del__,沒有無限遞歸,通過點(diǎn)表示法訪問。


查看完整回答
反對(duì) 回復(fù) 2021-12-17
?
素胚勾勒不出你

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

愚蠢,但我找到了更優(yōu)雅的方法來解決我的問題。


class Config(object):

    config = dict()


    def __getattribute__(self, name):

        if name in Config.config:

            return Config.config[name]

        else:

            raise AttributeError("No parameter named [%s]" % name)


    def __init__(self, file):

        try:

            with open(file, 'r') as f:

                for config_line in f:

                    config_line = config_line.strip()

                    eq_position = config_line.find('=')

                    key = config_line[:eq_position].strip()

                    value = config_line[eq_position + 1:].strip()

                    Config.config[key] = value

        except FileNotFoundError:

            print('File not found')


查看完整回答
反對(duì) 回復(fù) 2021-12-17
  • 2 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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