python 中类的__dict__属性和类对象的__dict_
2019-03-08 本文已影响0人
wangcc_sd
# -*- coding: utf-8 -*-
class A(object):
"""
Class A.
"""
a = 0
b = 1
def __init__(self):
self.a = 2
self.b = 3
def test(self):
print 'a normal func.'
@staticmethod
def static_test(self):
print 'a static func.'
@classmethod
def class_test(self):
print 'a calss func.'
obj = A()
print A.__dict__
print obj.__dict__
运行结果如下:
{'a': 0, '__module__': '__main__', 'b': 1, 'class_test': <classmethod object at 0x00000000021882E8>, '__dict__': <attribute '__dict__' of 'A' objects>, '__init__': <function __init__ at 0x00000000023A5BA8>, 'test': <function test at 0x00000000023A5C18>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': '\n Class A.\n ', 'static_test': <staticmethod object at 0x00000000021881C8>}
{'a': 2, 'b': 3}
由此可见, 类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类dict里的
对象的dict中存储了一些self.xxx的一些东西