• 技术文章 >Python技术 >Python基础教程

    python __dict__的使用注意

    小妮浅浅小妮浅浅2021-10-22 10:00:35原创4185

    1、__dict__是属性,不是方法。

    2、若使用实例对象调用__dict__,则输出由所有实例属性组成的字典。

    3、用类对象调用__dict__将输出由所有实例方法、类属性和类方法组成的字典。

    实例

    class PoloBlog:
        sum = 0
     
        def __init__(self, name):
            self.name = name
     
        def test(self):
            pass
     
        @classmethod
        def test_cls(cls):
            pass
     
        @staticmethod
        def test_static():
            pass
     
     
    blog = PoloBlog("小菠萝")
    blog.test()
    # 实例对象调用
    print(blog.__dict__)
     
    # 类对象调用
    print(PoloBlog.__dict__)
     
     
    # 输出结果
    {'name': '小菠萝'}
    {'__module__': '__main__', 'sum': 0, '__init__': <function PoloBlog.__init__ at 0x105d2b0d0>, 'test': <function PoloBlog.test at 0x105d4d310>, 'test_cls': <classmethod object at 0x105c47fa0>, 'test_static': <staticmethod object at 0x105c47d90>, '__dict__': <attribute '__dict__' of 'PoloBlog' objects>, '__weakref__': <attribute '__weakref__' of 'PoloBlog' objects>, '__doc__': None}

    以上就是python __dict__的使用注意,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python __dict__
    品易云
    上一篇:python zip_longest和zip的比较 下一篇:python有哪些字符串查找类方法

    相关文章推荐

    • python requests重定向的操作• python requests的超时使用• python捕获异常的原因• python创建平衡二叉树的方法• python如何配置文件路径• python字典的底层原理• python函数中的形参有几种• python函数实参的四种类型• python变量名的查找方法• python格式化经纬度的方法• python多进程如何优化显示进度条• python多进程中多个参数函数的使用• python中format_map的使用• python zip_longest和zip的比较

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网