python的的

python的的,第1张

 将字典转换成对象的小技巧

bokeyuan = {"b": 1,
                "o": 2,
                "k": 3,
                "e": 4,
                "y": 5,
                "u": 6,
                "a": 7,
                "n": 8,
                }

class Dict2Obj:

    # def __init__(self, bokeyuan):
    #     self.b = bokeyuan['b']
    #     self.o = bokeyuan['o']
    #     self.k = bokeyuan['k']
    #     self.e = bokeyuan['e']
    #     self.y = bokeyuan['y']
    #     self.u = bokeyuan['u']
    #     self.a = bokeyuan['a']
    #     self.n = bokeyuan['n']

    def __init__(self, bokeyuan):
        print("修改前:", self.__dict__)
        self.__dict__.update(bokeyuan)
        print("修改后:", self.__dict__)

d = Dict2Obj(bokeyuan)
print(d)
D:\MC\venv\Scripts\py

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/727985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存