html5/css教程

javascript教程

asp教程

php教程

jsp教程

C#/.NET教程

Python教程

网络营销

建站经验

点击排行

Python3中的type和object用法

来源:中文源码网    浏览:652 次    日期:2024-03-27 17:50:52
中文源码网举例说明:Python中的type和object的用法
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)))
打印结果如下:
type a:
type int:
type b:
type str:
在python中是一切皆对象的,类其实也是对象,首先type生成了这个对象,又生成了1这个对象,type --> int --> 1

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


object   所有类的最顶层基类是object
print("int 的基类是:{}".format(int.__bases__))
print("str 的基类是:{}".format(str.__bases__))
打印结果如下:
int 的基类是:(,)
str 的基类是:(,)
的基类都是 即:object是最顶层的基类
type与object的关系(type的基类是object,object是type生成的,object的基类为空)
print("type 的基类是:{}".format(type.__bases__))
print("type object:{}".format(type(object)))
print("object 的基类是:{}".format(object.__bases__))

打印结果如下:

type 的基类是:(,)

type object:
object 的基类是:()

精彩推荐