01.Python中一切皆对象

151 阅读1分钟

目录

学习资料

coding.imooc.com/class/200.h…

一.Python中一切皆对象

1.0 一切皆对象

Python中函数和类也是对象

  1. 可以赋值给变量
  2. 可以添加到集合对象中
  3. 可以作为参数传递给函数
  4. 可以当做函数的返回值

1.1 type object class之间的关系

type -> class -> object

list... 也是type的实例又是一个类

图片来源于慕课网课程-type-class-object的关系.png

#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
"""
@project: collections_mkw
@author: zy7y
@file: type_obejct_class.py
@ide: PyCharm
@time: 2020/9/13

关系
1. object是所有类的基类
2. object是type的实例 
3. type的实例 是自己本身
4. type继承object
"""
a = 1
print(type(a))  # <class 'int'>

print(type(int))  # <class 'type'>

class Student():
    pass

s = Student()

# 查看基类
print(Student.__bases__)  # (<class 'object'>,)
print(type(Student))    # <class 'type'>
print(type.__bases__)   # (<class 'object'>,)
print(type(object))     # <class 'type'>
print(type(type))       # <class 'type'>

1.2 Python中的常见内置类型

  • 对象的三个特征

    • 身份(内存地址,Python中使用id()函数查看 )

      print(id(11))  # 4491123808
      
    • 类型

      • None(Java语言中null)

      • 数值

      • 迭代类型

      • 序列类型

      • 映射(dict)

      • 集合

      • 上下文管理(with)

      • 其他

        • 模块
        • class和实例
        • 函数
        • 方法
        • 代码
        • object对象
        • type类型
        • ellipsis类型
        • notimplemented类型
      • 上面代码实例11就是值

ps:关于内置类型需要学习后面章节才能接触到~~