• Python __dict__



    一、无处不在的__dict__

    类的__dict__、类对象的__dict__

    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():
            print('a static func.')
    
        @classmethod
        def class_test(cls):
            print('a class func.')
    
    obj = A()
    print(A.__dict__)
    print(obj.__dict__)
    

    输出:

    {'__module__': '__main__', 'a': 0, 'b': 1, '__init__': <function A.__init__ at 0x00000169C2E452F0>, 'test': <function A.test at 0x00000169C2E45400>, 'static_test': <staticmethod object at 0x00000169C2E494A8>, 'class_test': <classmethod object at 0x00000169C2E494E0>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
    {'a': 2, 'b': 3}
    

    类的静态函数、类函数、普通函数、全局变量、一些内置的属性都放在__dict__里。

    对象__dict__中存储的是一些self.xxx



    二、Python里什么没有__dict__属性

    一些内置的数据类型没有__dict__属性。

    num = 3
    lst = []
    dict = {}
    print(num.__dict__)
    print(lst.__dict__)
    print(dict.__dict__)
    

    输出:

    AttributeError: 'list' object has no attribute '__dict__'
    AttributeError: 'list' object has no attribute '__dict__'
    AttributeError: 'dict' object has no attribute '__dict__'
    


    三、继承时的__dict__属性

    子类有自己的__dict__,父类有自己的__dict__

    子类的全局变量和函数放在子类的dict中,父类的放在父类dict中。

    class Parent():
        a = 0
        b = 1
    
        def __init__(self):
            self.a = 2
            self.b = 3
    
        def p_test(self):
            pass
    
    class Child(Parent):
        a = 4
        b = 5
    
        def __init__(self):
            super().__init__()
    
        def c_test(self):
            pass
    
        def p_test(self):
            pass
    
    p = Parent()
    c = Child()
    print(Parent.__dict__)
    print(Child.__dict__)
    print(p.__dict__)
    print(c.__dict__)
    

    输出:

    {'__module__': '__main__', 'a': 0, 'b': 1, '__init__': <function Parent.__init__ at 0x00000291EFD052F0>, 'p_test': <function Parent.p_test at 0x00000291EFD05400>, '__dict__': <attribute '__dict__' of 'Parent' objects>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None}
    {'__module__': '__main__', 'a': 4, 'b': 5, '__init__': <function Child.__init__ at 0x00000291F0D4BB70>, 'c_test': <function Child.c_test at 0x00000291F0D4BBF8>, 'p_test': <function Child.p_test at 0x00000291F0D4BC80>, '__doc__': None}
    {'a': 2, 'b': 3}
    {'a': 2, 'b': 3}
    

    总结:

    • 内置的数据类型没有__dict__属性。
    • 每个类有自己的__dict__属性,就算存着继承关系,父类的__dict__ 并不会影响子类的__dict__。
    • 对象也有自己的__dict__属性, 存储self.xxx 信息,父子类对象公用__dict__。


  • 相关阅读:
    [译]6.1. Data Structures Featured in This Chapter 本章涉及到的数据结构
    Linux中进程结构描述符
    How to uninstall GRUB
    [每日一点]msgsnd函数代码跟踪
    开始从代码入手学习内核
    剖析MagicAjax
    Castle实践6-TypedFactory Facility
    移植MSPetShop3到Castle MonoRail -Model与DAL层的移植(AR)
    热血江湖外挂之【热血江湖自补器 Version 0.1】
    对 "闭包closure" 的一些见解
  • 原文地址:https://www.cnblogs.com/keye/p/15672375.html
Copyright © 2020-2023  润新知