Python __dict__属性:查看对象内部所有属性名和属性值组成的字典
为了方便用户查看类中包含哪些属性,Python 类提供了 __dict__ 属性。需要注意的一点是,该属性可以用类名或者类的实例对象来调用,用类名直接调用 __dict__,会输出该由类中所有类属性组成的字典;而使用类的实例对象调用 __dict__,会输出由类中所有实例属性组成的字典。
举个例子:
class CLanguage:
a = 1
b = 2
def __init__ (self):
self.name = "C语言中文网"
self.add = "http://c.biancheng网站站点" rel="nofollow" />
{'__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网站站点" rel="nofollow" />
class CLanguage:
a = 1
b = 2
def __init__ (self):
self.name = "C语言中文网"
self.add = "http://c.biancheng网站站点" rel="nofollow" />
{'__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网站站点" rel="nofollow" />class CLanguage:
a = "aaa"
b = 2
def __init__ (self):
self.name = "C语言中文网"
self.add = "http://c.biancheng网站站点" rel="nofollow" />
{'name': 'C语言中文网', 'add': 'http://c.biancheng网站站点" rel="nofollow" />