python类中的__dict__

直接上代码:

class Person(object):                      
    name = 'python'                        
    age = 18                               
                                           
    def __init__(self):                    
        self.sex = 'boy'                   
        self.like = 'papapa'               
                                           
    @staticmethod                          
    def stat_func():                       
        print 'this is stat_func'          
                                           
    @classmethod                           
    def class_func(cls):                   
        print 'class_func'                 
                                           
                                           
person = Person()                          
print 'Person.__dict__: ', Person.__dict__ 
print 'person.__dict__: ', person.__dict__ 

运行结果:

Person.__dict__:  {'__module__': '__main__', 'name': 'python', '__init__': <function __init__ at 0x000000000385B518>, 'class_func': <classmethod object at 0x0000000003847F78>, '__dict__': <attribute '__dict__' of 'Person' objects>, 'age': 18, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'stat_func': <staticmethod object at 0x00000000037CFAF8>}
person.__dict__:  {'like': 'papapa', 'sex': 'boy'}

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

而实例对象中存储了一些self.xxx的一些东西

在类的继承中,子类有自己的__dict__, 父类也有自己的__dict__,子类的全局变量和方法放在子类的dict中,父类的放在父类dict中。

class Person(object):                          
    name = 'python'                            
    age = 18                                   
                                               
    def __init__(self):                        
        self.sex = 'boy'                       
        self.like = 'papapa'                   
                                               
    @staticmethod                              
    def stat_func():                           
        print 'this is stat_func'              
                                               
    @classmethod                               
    def class_func(cls):                       
        print 'class_func'                     
                                               
                                               
class Hero(Person):                            
    name = 'super man'                         
    age = 1000                                 
                                               
    def __init__(self):                        
        super(Hero, self).__init__()           
        self.is_good = 'yes'                   
        self.power = 'fly'                     
                                               
                                               
person = Person()                              
print 'Person.__dict__: ', Person.__dict__     
print 'person.__dict__: ', person.__dict__     
                                               
hero = Hero()                                  
print 'Hero.__dict__: ', Hero.__dict__         
print 'hero.__dict__: ', hero.__dict__         

运行结果:

Person.__dict__:  {'__module__': '__main__', 'name': 'python', '__init__': <function __init__ at 0x000000000374B518>, 'class_func': <classmethod object at 0x0000000003750048>, '__dict__': <attribute '__dict__' of 'Person' objects>, 'age': 18, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None, 'stat_func': <staticmethod object at 0x0000000003737FD8>}
person.__dict__:  {'like': 'papapa', 'sex': 'boy'}
Hero.__dict__:  {'age': 1000, '__doc__': None, '__module__': '__main__', '__init__': <function __init__ at 0x000000000374B668>, 'name': 'super man'}
hero.__dict__:  {'is_good': 'yes', 'like': 'papapa', 'power': 'fly', 'sex': 'boy'}

从运行结果可以看出,类对象的__dict__虽然没有继承父类的,但是实例对象继承了父类的实例属性

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值