类、字典、自我、初始化、参数?
class attrdict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **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):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **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
Could somebody explain the first four lines in words? I read about classes and methods. But here it seems very confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我逐行解释:
这一行将类 attrdict 声明为内置 dict 类的子类。
这是您的标准
__init__
方法。对dict.__init__(...)
的调用是利用 superclass'(在本例中为 dict)构造函数 (
__init__
) 方法。最后一行,
self.__dict__ = self
使得传递给__init__
方法的关键字参数 (kwargs) 可以像属性一样被访问,即 ax, ay在下面的代码中。希望这有助于消除您的困惑。
My shot at a line-by-line explanation:
This line declares a class attrdict as a subclass of the built-in dict class.
This is your standard
__init__
method. The call todict.__init__(...)
is to utilize the superclass' (in this case, dict) constructor (
__init__
) method.The final line,
self.__dict__ = self
makes it so the keyword-arguments (kwargs) you pass to the__init__
method can be accessed like attributes, i.e., a.x, a.y in the code below.Hope this helps clear up your confusion.
您在示例中没有使用位置参数。所以相关的代码是:
在第一行中,您将类
attrdict
定义为dict
的子类。在第二行中,您定义自动初始化实例的函数。您将关键字参数 (
**kargs
) 传递给此函数。当你实例化a
时:你实际上是在调用
dict实例,核心初始化是通过初始化
dict
内置超类来完成的。这是在第三行中完成的,传递 attrdict.__init__ 中接收的参数。因此,
使
self
(实例a
)成为一个字典:最后一行发生了好事:
每个实例都有一个保存其属性的字典。这是
self.__dict__
(即a.__dict__
)。例如,如果
我们可以编写
ax
或ay
并分别获取值 1 或 2。所以,这就是第 4 行的作用:
相当于:
然后我可以调用
ax
和ay
。希望不要太乱。
You are not using positional arguments in your example. So the relevant code is:
In the first line you define class
attrdict
as a subclass ofdict
.In the second line you define the function that automatically will initialize your instance. You pass keyword arguments (
**kargs
) to this function. When you instantiatea
:you are actually calling
dict instance core initialization is done by initializing the
dict
builtin superclass. This is done in the third line passing the parameters received inattrdict.__init__
.Thus,
makes
self
(the instancea
) a dictionary:The nice thing occurs in the last line:
Each instance has a dictionary holding its attributes. This is
self.__dict__
(i.e.a.__dict__
).For example, if
we could write
a.x
ora.y
and get values 1 or 2, respectively.So, this is what line 4 does:
is equivalent to:
Then I can call
a.x
anda.y
.Hope is not too messy.
这是一篇解释
__dict__
的好文章:动态dict
attrdict
类通过从字典继承然后将对象的__dict__
设置为该字典来利用这一点。因此,任何属性访问都是针对父字典(即它继承的 dict 类)进行的。本文的其余部分对于理解 Python 对象也非常有用:
Python 属性和方法
Here's a good article that explains
__dict__
:The Dynamic dict
The
attrdict
class exploits that by inheriting from a dictionary and then setting the object's__dict__
to that dictionary. So any attribute access occurs against the parent dictionary (i.e. thedict
class it inherits from).The rest of the article is quite good too for understanding Python objects:
Python Attributes and Methods