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

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

您如何解包方法參數(shù)以為其分配類(lèi)屬性?

您如何解包方法參數(shù)以為其分配類(lèi)屬性?

Qyouu 2021-06-10 18:17:15
我經(jīng)常做這樣的事情:class Box:    def __init__(self):        some_setup_stuff()    def configure(        self,        color               = "#ffffff",        weight              = 1,        empathy             = 97,        angle_x             = 0,        angle_y             = 0,        angle_z             = 0,        displacement_x      = 0,        displacement_y      = 0,        displacement_z      = 0        ):        self.color          = color        self.weight         = weight        self.empathy        = empathy        self.angle_x        = angle_x        self.angle_y        = angle_y        self.angle_z        = angle_z        self.displacement_x = displacement_x        self.displacement_y = displacement_y        self.displacement_z = displacement_z    def open(self):        reveal_head()是否有一些簡(jiǎn)潔、小巧、相當(dāng)合理的方法可以將傳遞給類(lèi)方法的參數(shù)“解包”到類(lèi)的屬性中(同時(shí)保持明確指定的默認(rèn)值)?就像,我在想也許locals()可以在方法的第一行周?chē)阅撤N方式使用,但對(duì)我來(lái)說(shuō)并不明顯。所以我們最終可能會(huì)得到這樣的結(jié)果:class Box:    def __init__(self):        some_setup_stuff()    def configure(        self,        color               = "#ffffff",        weight              = 1,        empathy             = 97,        angle_x             = 0,        angle_y             = 0,        angle_z             = 0,        displacement_x      = 0,        displacement_y      = 0,        displacement_z      = 0        ):        # magic possibly involving locals()    def open(self):        reveal_head()它可以像這樣使用:>>> box = Box()>>> box.configure(empathy = 98)>>> box.weight1>>> box.empathy98
查看完整描述

1 回答

?
四季花海

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

這是一個(gè)有點(diǎn)hacky的方法。構(gòu)建一個(gè)defaults包含允許參數(shù)默認(rèn)值的字典。然后更新self.__dict__與**kwargs,在按鍵上的一些錯(cuò)誤檢查后:


class Box:

    def __init(self):

        pass

    def configure(self, **kwargs):

        defaults = {

            "color": "#ffffff",

            "weight": 1,

            "empathy": 97,

            "angle_x": 0,

            "angle_y": 0,

            "angle_z": 0,

            "displacement_x": 0,

            "displacement_y": 0,

            "displacement_z": 0

        }

        bad_args = [k for k in kwargs if k not in defaults]

        if bad_args:

            raise TypeError("configure() got unexpected keyword arguments %s"%bad_args)

        self.__dict__.update(defaults)

        self.__dict__.update(kwargs)

現(xiàn)在你可以這樣做:


box = Box()

box.configure(empathy = 98)

print(box.weight)

#1

print(box.empathy)

#98

但如果你這樣做了:


box.configure(wieght = 2)

#TypeError: configure() got unexpected keyword arguments ['wieght']


查看完整回答
反對(duì) 回復(fù) 2021-06-22
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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