详解Python中的type和object

yipeiwu_com6年前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 模块EasyGui详细介绍

Python 模块EasyGui详细介绍 前言: 在Windows想用Python开发一些简单的界面,所以找到了很容易上手的EasyGui库。下面就分享一下简单的使用吧。 参考的链接:官...

Python Django 命名空间模式的实现

Python Django 命名空间模式的实现

新建一个项目 app02 在 app02/ 下创建 urls.py: from django.conf.urls import url from app02 import view...

pandas.DataFrame.to_json按行转json的方法

最近需要将csv文件转成DataFrame并以json的形式展示到前台,故需要用到Dataframe的to_json方法 to_json方法默认以列名为键,列内容为值,形成{col1:[...

python 用所有标点符号分隔句子的示例

问题 给出一段话,由短句组成,短句之间可能被任意标点符号隔开。想要提取所有的短句。 解决 使用 re.split 函数,用正则式匹配的方法,一次性分隔所有短句。 import re...

简单介绍Python中的RSS处理

RSS 是一个可用多种扩展来表示的缩写:“RDF 站点摘要(RDF Site Summary)”、“真正简单的辛迪加(Really Simple Syndication)”、“丰富站点摘...