Python 中的__dict__

1、无处不在的__dict__

  首先看一下类的__dict__属性和类对象的__dict__属性

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

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


class A(object):
    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}

 

2、Python里什么没有__dict__属性

虽然说一切皆对象,但对象也有不同,就好比不是每个人的女朋友都是一个人一样,一些内置的数据类型是没有__dict__属性的,如下:

int, list, dict等这些常用的数据类型是没有__dict__属性的,其实这是可预料的,就算给了它们dict属性也没啥用,毕竟它们只是用来做数据容器的。

 

3、发生继承时候的__dict__属性

  子类有自己的__dict__, 父类也有自己的__dict__,继承之后会是什么样呢。

  1)下段输出结果中,用红色字体标出的是类变量和函数,由结果可知,每个类的类变量、函数名都放在自己的__dict__中

  2) 再来看一下实例变量的__dict__中,由蓝色字体标识,父类和子类对象的__dict__是公用的

class Parent(object):
    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(Child, self).__init__()
        # self.a = 6
        # self.b = 7
    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__


{'a': 0, '__module__': '__main__', 'b': 1, '__dict__': <attribute '__dict__' of 'Parent' objects>, 'p_test': <function p_test at 0x0000000002325BA8>, '__weakref__': <attribute '__weakref__' of 'Parent' objects>, '__doc__': None, '__init__': <function __init__ at 0x0000000002325B38>}
{'a': 4, 'c_test': <function c_test at 0x0000000002325C88>, '__module__': '__main__', 'b': 5, 'p_test': <function p_test at 0x0000000002325CF8>, '__doc__': None, '__init__': <function __init__ at 0x0000000002325C18>}
{'a': 2, 'b': 3}
{'a': 2, 'b': 3}

 

总结:

  1) 内置的数据类型没有__dict__属性

  2) 每个类有自己的__dict__属性,就算存着继承关系,父类的__dict__ 并不会影响子类的__dict__

  3) 对象也有自己的__dict__属性, 存储self.xxx 信息,父子类对象公用__dict__

__dict__一个小妙用:

快速初始化未知个数自典型参数

class Book(object):
    def __init__(self, name, authors, price, **rest):
        '''rest的例子有:出版商、长度、标签、出版日期'''
        self.name = name
        self.authors = authors
        self.price = price
        self.__dict__.update(rest)

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值