class CLanguage: a = 1 b = 2 def __init__ (self): self.name = "C语言中文网" self.add = "http://task.lmcjl.com" #通过类名调用__dict__ print(CLanguage.__dict__) #通过类实例对象调用 __dict__ clangs = CLanguage() print(clangs.__dict__)程序输出结果为:
{'__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://task.lmcjl.com'}
class CLanguage: a = 1 b = 2 def __init__ (self): self.name = "C语言中文网" self.add = "http://task.lmcjl.com" class CL(CLanguage): c = 1 d = 2 def __init__ (self): self.na = "Python教程" self.ad = "http://task.lmcjl.com/python" #父类名调用__dict__ print(CLanguage.__dict__) #子类名调用__dict__ print(CL.__dict__) #父类实例对象调用 __dict__ clangs = CLanguage() print(clangs.__dict__) #子类实例对象调用 __dict__ cl = CL() print(cl.__dict__)运行结果为:
{'__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://task.lmcjl.com'}
{'na': 'Python教程', 'ad': 'http://task.lmcjl.com/python'}
class CLanguage: a = "aaa" b = 2 def __init__ (self): self.name = "C语言中文网" self.add = "http://task.lmcjl.com" #通过类实例对象调用 __dict__ clangs = CLanguage() print(clangs.__dict__) clangs.__dict__['name'] = "Python教程" print(clangs.name)程序运行结果为:
{'name': 'C语言中文网', 'add': 'http://task.lmcjl.com'}
Python教程
本文链接:http://task.lmcjl.com/news/7323.html