zoukankan      html  css  js  c++  java
  • python笔记58-类里面的__dict__属性

    前言

    python是面向对象的,对象有属性和方法,可以通过__dict__查看对象的属性

    __dict__查看对象属性

    首先弄清楚2个概念,类(A)和类的实例对象(A()), 如下代码

    • count 是A的类属性
    • name和age是 A类的实例对象A()属性
    • start 是实例方法,默认带self参数
    • stop 是静态方法,可以不带默认参数
    • open 是类方法,默认带cls参数
    class A(object):
        count = 0
    
        def __init__(self):
            self.name = "yoyo"
            self.age = 18
    
        def start(self):
            """实例方法"""
            print("start-11111")
    
        @staticmethod
        def stop():
            """静态方法"""
            print("stop-22222")
    
        @classmethod
        def open(cls):
            print("open-3333333")
    

    A类有属性和方法,抽象的来讲,方法也可以看成类的属性(方法属性)

    print(A.__dict__)  # A类属性
    a = A()    # a是A类的实例对象
    print(a.__dict__)   # A类的实例对象属性
    

    运行结果

    {'__module__': '__main__', 'count': 0, '__init__': <function A.__init__ at 0x000001F84781AAE8>, 'start': <function A.start at 0x000001F84781AB70>, 'stop': <staticmethod object at 0x000001F84783A2B0>, 'open': <classmethod object at 0x000001F84783A2E8>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
    {'name': 'yoyo', 'age': 18}
    

    从运行结果可以看出,A的类属性有count,还有定义的一些方法(__init__构造方法,还有实例方法,静态方法,类方法)
    A()实例对象只有__init__构造方法里面的name和age属性(count是类属性,并不是类的实例对象属性)

    如果我们直接A.name 和 A.age就会报错

    print(A.name)
    print(A.age)
    

    报错

    Traceback (most recent call last):
      File "D:/wangyiyun_hrun3/demo/a.py", line 27, in <module>
        print(A.name)
    AttributeError: type object 'A' has no attribute 'name'
    

    因为name和age属性在__init__构造方法里面,只有当A类实例化的时候,才会执行__init__构造方法,这时候才会有name和age属性了

    继承时__dict__属性

    当B类继承A类的时候,A类和B类都有自己的类属性 count,也各自有自己的__init__构造方法

    class A(object):
        count = 0
    
        def __init__(self):
            self.name = "yoyo"
            self.age = 18
    
        def start(self):
            """实例方法"""
            print("start-11111")
    
        @staticmethod
        def stop():
            """静态方法"""
            print("stop-22222")
    
        @classmethod
        def open(cls):
            print("open-3333333")
    
    
    class B(A):
        count = 22
    
        def __init__(self):
            super().__init__()
            self.name = "hello"
            self.age = 22
        def new(self):
            print("new--44444")
    
    print(A.__dict__)
    print(B.__dict__)
    a = A()
    b = B()
    print(a.__dict__)
    print(b.__dict__)
    

    运行结果

    {'__module__': '__main__', 'count': 0, '__init__': <function A.__init__ at 0x000001FD03F8AAE8>, 'start': <function A.start at 0x000001FD03F8AB70>, 'stop': <staticmethod object at 0x000001FD03FAA470>, 'open': <classmethod object at 0x000001FD03FAA4A8>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
    {'__module__': '__main__', 'count': 22, '__init__': <function B.__init__ at 0x000001FD03F8AD08>, 'new': <function B.new at 0x000001FD03F8AD90>, '__doc__': None}
    {'name': 'yoyo', 'age': 18}
    {'name': 'hello', 'age': 22}
    

    从运行结果可以看出

    • A类和B类的类属性count是不一样的,
    • 虽然B类继承了A类,方法属性也不一样,可以清楚的区分出哪些是A类的方法属性,哪些是B类的方法属性
  • 相关阅读:
    Unity3d限制帧数
    Linux 后台运行程序
    Flask异步发送邮件的方法
    [转]视图多表
    dede 织梦手机静态化一键生成插件
    如何在wordpress中设置关键词和描述
    前端PS切图
    帝国cms用自定义列表页做首页
    帝国cms 滚动加载更多整合
    swiper 自定义pagination 样式内容
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/15151723.html
Copyright © 2011-2022 走看看