现在的位置: 首页 > 综合 > 正文

Python 中的__dict__

2018年02月14日 ⁄ 综合 ⁄ 共 332字 ⁄ 字号 评论关闭

python中,__dict__是干什么用的呢?其实它提供给函数增加属性的功能

例子:

    class Person:  
    def __init__(self,_obj):
        self.name = _obj['name']  
        self.age = _obj['age']  
        self.energy = _obj['energy']  
        self.gender = _obj['gender']  
        self.email = _obj['email']  
        self.phone = _obj['phone']  
        self.country = _obj['country']  


是不是很繁琐?用__dict__一句话就可以搞定

class Person:  
    def __init__(self,_obj):  
        self.__dict__.update(_obj)

抱歉!评论已关闭.