python C 类,dict,self,init,args?
发布时间:2023-12-16 23:30:47 所属栏目: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. 希望这有助于清除你的困惑. (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python中的全局变量混淆
- python – 如何在DataFrame中增加groupby中的行数
- python – 使用“type”作为属性名称是一种不好的做法?
- python – 如何为Xerox打印机创建动态作业单?
- python – 用py2exe创建的可执行文件不能在windows xp 32bi
- python – Linux中的Tkinter外观(主题)
- python – UnicodeDecodeError:’utf8’编解码器无法解码位
- python – 如何串流叽叽叽t witter y y y?
- 使用Python全局变量的动态类的最佳方法()
- django – XSRF标题未在AngularJS中设置
推荐文章
站长推荐