python C 类,dict,self,init,args?
发布时间:2023-12-16 23:30:10 所属栏目:Python 来源:DaWei
导读: class attrdict(dict):
def __init__(self,*args,**kwargs):
dict.__init__(self,**kwargs)
self.__dict__ = self
a = attrdict(x=1,y=2)
print a.x,a.y
b = attrdic
def __init__(self,*args,**kwargs):
dict.__init__(self,**kwargs)
self.__dict__ = self
a = attrdict(x=1,y=2)
print a.x,a.y
b = attrdic
class attrdict(dict): def __init__(self,*args,**kwargs): dict.__init__(self,**kwargs) self.__dict__ = self a = attrdict(x=1,y=2) print a.x,a.y b = attrdict() b.x,b.y = 1,2 print b.x,b.y 有人可以用文字解释前四行吗?我阅读了关于课堂和方法.但这里似乎很混乱. 解决方法 我的一枪一行解释: class attrdict(dict):该行声明一个类attrdict作为内置dict类的子类. def __init__(self,**kwargs): dict.__init__(self,**kwargs)这是您的标准__init__方法.调用dict .__ init __(…)是利用超级 最后一行,self .__ dict__ = self使得你传递给__init__方法的关键字参数(kwargs)可以像属性一样被访问,比如下面代码中的a.x,a.y. 希望这有助于清除你的困惑. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐