详解Python中的type和object

yipeiwu_com5年前Python基础

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设计】网站的支持!

相关文章

详解Python中映射类型的内建函数和工厂函数

1.基本函数介绍 (1)标准类型函数[type()、str()和 cmp()]         对一个字典调用typ...

Python 比较两个数组的元素的异同方法

通过set()获取两个数组的交/并/差集: print set(a).intersection(set(b)) # 交集 print set(a).union(set(b)) # 并...

分数霸榜! python助你微信跳一跳拿高分

前言 最近微信的跳一跳很火,大家看到排行榜上几百上千的分数,再看看自己百分左右的分数肯定很难过,我手残怪我吗?没关系,如果你跟着我来,也能让你分数霸榜。 原理 首先大家是有一个直观感受,...

python3实现跳一跳点击跳跃

借鉴了网上一些大神的代码和思路,这里整理一下写出点击跳跃玩跳一跳这个小游戏的思路 一、整体思路 棋子所在的坐标以及下一步所要到的坐标,根据两个坐标计算出两点之间距离进行跳跃。 二、分布思...

ubuntu环境下python虚拟环境的安装过程

一. 虚拟环境搭建 在开发中安装模块的方法: pip install 模块名称 之前我们安装模块都是直接在物理环境下安装,这种安装方法,后面一次安装的会覆盖掉前面一次安装的。那如果一台机...