zoukankan      html  css  js  c++  java
  • Python中type和object

    type  所有类是type生成的

    a = 1
    b = "abc"
    print("type a:{}".format(type(a)))
    print("type int:{}".format(type(int)))
    print("type b:{}".format(type(b)))
    print("type str:{}".format(type(str)))
    
    
    result:
    type a:<class 'int'>
    type int:<class 'type'>
    type b:<class 'str'>
    type str:<class 'type'>

    在python中是一切皆对象的,类其实也是对象,首先type生成了<class 'int'>这个对象,<class 'int'>又生成了1这个对象,type --> int --> 1

    同样,type生成了<class 'str'>这个对象,<class 'type'>又生成了"abc"这个对象,type --> str--> “abc”,即type -->生成类对象 -->对象

    object   所有类的最顶层基类是object

    print("int 的基类是:{}".format(int.__bases__))
    print("str 的基类是:{}".format(str.__bases__))
    
    
    result:
    int 的基类是:(<class 'object'>,)
    str 的基类是:(<class 'object'>,)

    <class 'int'>和<class 'str'>的基类都是 <class 'object'>  即:object是最顶层的基类

    type与object的关系(type的基类是object,object是type生成的,object的基类为空)

    print("type 的基类是:{}".format(type.__bases__))
    print("type object:{}".format(type(object)))
    print("object 的基类是:{}".format(object.__bases__))
    
    result:
    type 的基类是:(<class 'object'>,)
    type object:<class 'type'>
    object 的基类是:()

  • 相关阅读:
    Linux下c程序的编译方法:
    Linux分区机制和常见命令
    java虚拟机内存大小调整:
    break、continue、return循环三剑客之异同
    Scanner中的nextInt()陷阱
    windbg无法下载符号文件
    各种函数调用约定及浮点数传参
    一个crackme的分析
    SetProcessAffinityMask的问题
    x64内联汇编注意点
  • 原文地址:https://www.cnblogs.com/FG123/p/9463707.html
Copyright © 2011-2022 走看看