彩虹然

rainbow-ran

Python3.7之类的属性

一、__dict__

程序使用 __dict__ 属性既可查看对象的所有内部状态,也可通过字典语法来访问或修改指定属性的值。

class A:

    a = 1
    b = 2

    def __init__(self):
        self.c = 3
        self.d = 4

    def test1(self):
        pass

    @classmethod
    def test2(self):
        pass

    @staticmethod
    def test3():
        pass


A_1 = A()
print(A_1.__dict__)
print(A.__dict__)

'''
{'c': 3, 'd': 4}
{'__module__': '__main__', 'a': 1, 'b': 2, '__init__': <function A.__init__ at 0x00000149BE8396A8>, 'test1': <function A.test1 at 0x00000149BE839730>, 'test2': <classmethod object at 0x00000149BE831B38>, 'test3': <staticmethod object at 0x00000149BE831B70>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
'''

由此可见, 类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类__dict__里的

对象的__dict__中存储了一些self.xxx的一些东西

posted @ 2020-01-17 11:23  彩虹然  阅读(296)  评论(0编辑  收藏  举报