Python __dict__属性:查看对象内部所有属性名和属性

2022-05-20  本文已影响0人  秣禾

Python 类的内部,无论是类属性还是实例属性,都是以字典的形式进行存储的,其中属性名作为键,而值作为该键对应的值。

为了方便用户查看类中包含哪些属性,Python 类提供了 dict 属性。需要注意的一点是,该属性可以用类名或者类的实例对象来调用,用类名直接调用 dict,会输出该由类中所有类属性组成的字典;而使用类的实例对象调用 dict,会输出由类中所有实例属性组成的字典。

举个例子:

<pre class="python sh_python snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

1.  class CLanguage:
2.  a = 1
3.  b = 2
4.  def __init__ (self):
5.  self.name = "C语言中文网"
6.  self.add = "http://c.biancheng.net"
7.  #通过类名调用__dict__
8.  print(CLanguage.__dict__)

10.  #通过类实例对象调用 __dict__
11.  clangs = CLanguage()
12.  print(clangs.__dict__)

</pre>

程序输出结果为:

{'module': 'main', 'a': 1, 'b': 2, 'init': <function CLanguage.init at 0x0000022C69833E18>, 'dict': <attribute 'dict' of 'CLanguage' objects>, 'weakref': <attribute 'weakref' of 'CLanguage' objects>, 'doc': None}
{'name': 'C语言中文网', 'add': 'http://c.biancheng.net'}

不仅如此,对于具有继承关系的父类和子类来说,父类有自己的 dict,同样子类也有自己的 dict,它不会包含父类的 dict。例如:

<pre class="python sh_python snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

1.  class CLanguage:
2.  a = 1
3.  b = 2
4.  def __init__ (self):
5.  self.name = "C语言中文网"
6.  self.add = "http://c.biancheng.net"

8.  class CL(CLanguage):
9.  c = 1
10.  d = 2
11.  def __init__ (self):
12.  self.na = "Python教程"
13.  self.ad = "http://c.biancheng.net/python"
14.  #父类名调用__dict__
15.  print(CLanguage.__dict__)
16.  #子类名调用__dict__
17.  print(CL.__dict__)

19.  #父类实例对象调用 __dict__
20.  clangs = CLanguage()
21.  print(clangs.__dict__)
22.  #子类实例对象调用 __dict__
23.  cl = CL()
24.  print(cl.__dict__)

</pre>

运行结果为:

{'module': 'main', 'a': 1, 'b': 2, 'init': <function CLanguage.init at 0x000001721A853E18>, 'dict': <attribute 'dict' of 'CLanguage' objects>, 'weakref': <attribute 'weakref' of 'CLanguage' objects>, 'doc': None}
{'module': 'main', 'c': 1, 'd': 2, 'init': <function CL.init at 0x000001721CD15510>, 'doc': None}
{'name': 'C语言中文网', 'add': 'http://c.biancheng.net'}
{'na': 'Python教程', 'ad': 'http://c.biancheng.net/python'}

显然,通过子类直接调用的 dict 中,并没有包含父类中的 a 和 b 类属性;同样,通过子类对象调用的 dict,也没有包含父类对象拥有的 name 和 add 实例属性。

除此之外,借助由类实例对象调用 dict 属性获取的字典,可以使用字典的方式对其中实例属性的值进行修改,例如:

<pre class="python sh_python snippet-formatted sh_sourceCode" style="margin: 0px; display: block; padding: 0px; font-size: 14px; line-height: 1.6em; color: rgb(102, 102, 102); white-space: pre-wrap; overflow-wrap: break-word; background: none; border: none; border-radius: 0px;">

1.  class CLanguage:
2.  a = "aaa"
3.  b = 2
4.  def __init__ (self):
5.  self.name = "C语言中文网"
6.  self.add = "http://c.biancheng.net"

8.  #通过类实例对象调用 __dict__
9.  clangs = CLanguage()
10.  print(clangs.__dict__)
11.  clangs.__dict__['name'] = "Python教程"
12.  print(clangs.name)

</pre>

程序运行结果为:

{'name': 'C语言中文网', 'add': 'http://c.biancheng.net'}
Python教程

注意,无法通过类似的方式修改类变量的值。

上一篇 下一篇

猜你喜欢

热点阅读