Python----type、object和class的关系

简介: Python----type、object和class的关系

1 type类生成具体的类,具体的类则生成具体的实例

type->int->1,type->str->"abc"

a=100
b="hello world"
print(type(a))
print(type(int))
print(type(b))
print(type(str))
AI 代码解读

执行结果如下:

<class 'int'>
<class 'type'>
<class 'str'>
<class 'type'>
AI 代码解读

2 对于自定义类,对象由自定义的类生成的,而自定义的类同样也是由type生成的

class Student:
    pass

stu=Student()
print(type(stu))
print(type(Student))
AI 代码解读

执行结果如下:

<class '__main__.Student'>
<class 'type'>
AI 代码解读

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

print(int.__bases__)
print(str.__bases__)

class People:
    pass

class Person(People):
    pass

print(Person.__bases__)
print(People.__bases__)
AI 代码解读

执行结果如下:

(<class 'object'>,)
(<class 'object'>,)
(<class '__main__.People'>,)
(<class 'object'>,)
AI 代码解读

4、object类是由type类实例化而来的,而type的基类又是object,而object的基类则为空

print(type(object))
print(type.__bases__)
print(object.__bases__)
AI 代码解读

执行结果:

<class 'type'>
(<class 'object'>,)
()
AI 代码解读

5 type,object 和class 的关系

  • object 是所有类的顶层基类
  • type 的基类也是object
  • object是type的实例
  • type 是 type的实例
        实例------->|
         |         |
         |         |<---实例-----|     
         |--------type        object<--继承---|
                   |----继承---->|            |
                   |                         |
                   |<----------实例----------list
                   |<----------实例----------str
                   |<----------实例----------dict
                   |<----------实例----------tuple             
AI 代码解读
目录
打赏
0
0
0
0
17
分享
相关文章
|
3月前
|
Python错误 - 'list' object is not callable 的问题定位与解决
出现编程问题并不可怕,关键在于是否可以从中学习与成长。遇到'list' object is not callable这样的错误,我们不仅需要学会应对,更需要了解其背后的原因,避免类似的问题再次出现。记住,Python的强大功能和灵活性同时也意味着我们需要对其理解更准确,才能更好的使用它。
389 70
|
9月前
|
Python Tricks: String Conversion(Every Class Needs a ___repr__)
Python Tricks: String Conversion(Every Class Needs a ___repr__)
64 5
|
9月前
|
[oeasy]python036_数据类型有什么用_type_类型_int_str_查看帮助
本文回顾了Python中`ord()`和`chr()`函数的使用方法,强调了这两个函数互为逆运算:`ord()`通过字符找到对应的序号,`chr()`则通过序号找到对应的字符。文章详细解释了函数参数类型的重要性,即`ord()`需要字符串类型参数,而`chr()`需要整数类型参数。若参数类型错误,则会引发`TypeError`。此外,还介绍了如何使用`type()`函数查询参数类型,并通过示例展示了如何正确使用`ord()`和`chr()`进行转换。最后,强调了在函数调用时正确传递参数类型的重要性。
82 3
|
9月前
|
Python Tricks--- Object Comparisons:“is” vs “==”
Python Tricks--- Object Comparisons:“is” vs “==”
56 1
|
11月前
|
python布尔类型 (Boolean Type)
【8月更文挑战第3天】
255 8
|
11月前
|
【Python】使用LogisticRegression出现错误: invalid type promotion
使用Python中的LogisticRegression时遇到TypeError: invalid type promotion错误的解决方法,指出错误原因是因为输入的DataFrame包含datetime类型的数据,并提供了通过删除datetime字段来解决此问题的步骤。
136 3
|
11月前
|
Python中映射类型 (Mapping Type)
【8月更文挑战第2天】
489 2
【Python】解决tqdm ‘module‘ object is not callable
在使用tqdm库时遇到的“'module' object is not callable”错误,并给出了正确的导入方式以及一些使用tqdm的常见示例。
314 1
【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
455 1
|
11月前
|
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问