zoukankan      html  css  js  c++  java
  • Python中type和object类的关系

    源码分析

    class type(object):
        """
        type(object_or_name, bases, dict)
        type(object) -> the object's type
        type(name, bases, dict) -> a new type
        """
        pass
     
    class object:
        """
        The base class of the class hierarchy.
        
        When called, it accepts no arguments and returns a new featureless
        instance that has no instance attributes and cannot be given any.
        """
        pass
      
    

    可以简单的看得,object是type的父类,那么type是继承object基类的。

    简单的输出

    print(type(type))
    print(type(object))
    
    # 输出结果
    # <class 'type'>
    # <class 'type'>
    

    那么说明type其实是类型的顶端,而object是类的顶端。

    总结

    • type类是数据类型的顶端,我们除了object的type也是type。
    • type类的父类是object,那么说明object类是继承类的顶端。
    • 构造数据类型需要使用到type类,那么如果我们想创建自己的自定义类就可以继承type实现创建自己的自定义类型,同时可以使用很多魔方方法来实现自己的类型的内容的封装。
    • 以后机会详细讲解一下type元类的使用,以及常用的场景。


    本文 FANDX 原创内容,未经允许禁止转发,违者必究其责任!
  • 相关阅读:
    [HAOI2008]硬币购物
    [SCOI2005]骑士精神
    [ZJOI2007]最大半联通子图
    [HAOI2007]反素数
    [SCOI2005]繁忙的都市
    小凯的疑惑
    5月16日vj题解
    周六题目前四题详解
    Codeforces Round #629 (Div. 3)做题记录
    Codeforces Round #570 (Div. 3) B. Equalize Prices
  • 原文地址:https://www.cnblogs.com/fandx/p/15032572.html
Copyright © 2011-2022 走看看