详解Python中的type和object

作者:Harvard_Fly 时间:2021-03-25 13:00:58 

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 的基类是:()

详解Python中的type和object 

总结

以上所述是小编给大家介绍的Python中type和object网站的支持!

来源:http://www.cnblogs.com/FG123/p/9463707.html

标签:python,type,object
0
投稿

猜你喜欢

  • PyQt5实现从主窗口打开子窗口的方法

    2023-01-14 11:06:11
  • wap开发中如何有效的利用缓存减少消息的传送量

    2022-12-16 04:23:17
  • python web框架 django wsgi原理解析

    2021-11-21 02:44:59
  • asp如何对Access数据库进行压缩?

    2009-11-19 21:20:00
  • Python3安装psycopy2以及遇到问题解决方法

    2022-12-19 15:41:26
  • Python运行提示缺少模块问题解决方案

    2023-06-24 02:16:23
  • SQLServer中的触发器基本语法与作用

    2024-01-25 18:18:02
  • 跟老齐学Python之用while来循环

    2021-01-13 23:38:18
  • mysql 控制台程序的提示符 prompt 字符串设置

    2024-01-26 01:42:11
  • 对python中类的继承与方法重写介绍

    2023-03-21 09:49:20
  • Python WSGI的深入理解

    2021-04-20 21:48:25
  • MySQL下海量数据的迁移步骤分享

    2024-01-16 13:13:10
  • 如何用python抓取B站数据

    2023-07-19 20:02:55
  • Python中字典和JSON互转操作实例

    2023-03-12 14:18:11
  • 理解和使用Oracle 8i分析工具LogMiner

    2010-07-16 13:22:00
  • asp里Sub与Function有什么区别

    2007-08-17 10:39:00
  • Python 图形界面框架TkInter之在源码中找pack方法

    2021-10-06 05:10:56
  • 使用php-timeit估计php函数的执行时间

    2023-10-07 19:56:50
  • Docker部署用Python编写的Web应用的实践

    2022-05-11 17:53:28
  • JS画5角星方法介绍

    2024-05-13 09:06:36
  • asp之家 网络编程 m.aspxhome.com