• Python一切皆对象-面向对象


    Python中一切皆对象,Python面向对象更彻底。

    一、函数和类也是对象,属于Python的一等公民

    1.可以赋值给一个变量
    def ask(name="coder"):
        print(name)
    
    class Person:
        def __init__(self):
            print("小明")
    # 函数赋值给变量后执行
    my_func = ask
    my_func("王尼玛") # 输出”王尼玛“
    
    # 类作为变量赋值给变量后实例化
    my_class = Person
    my_class() # 会执行__init__()方法,输出“小明”
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    2.可以增加到集合对象中
    def ask(name="小明"):
        print(name)
    
    class Person:
        def __init__(self):  # 不返回实例,返回的类对象
            print("小明")
    
    def print_type(item):
        print(type(item))
    
    # 类和函数增加到集合对象
    obj_list = []
    obj_list.append(ask)
    obj_list.append(Person)
    for item in obj_list:
        print(item())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    3.可以作为参数传递给函数
    def ask(name="小明"):
        print(name)
    
    class Person:
        def __init__(self):  # 不返回实例,返回的类对象
            print("小明")
    
    def print_type(item):
        print(type(item))
    
    # 类和函数增加到集合对象
    obj_list = []
    obj_list.append(ask)
    obj_list.append(Person)
    for item in obj_list:
        print_type(item)  # 作为参数传入函数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    4.可以当做函数的返回值
    def ask(name="小明"):
        print(name)
    
    class Person:
        def __init__(self):  # 不返回实例,返回的类对象
            print("小明")
    
    def decorator_func():
        print("dec start")
        return ask  # 作为函数的返回值,Python装饰器的部分实现原理
    
    my_ask = decorator_func()
    my_ask("tom")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二、type、object和class的关系

    原来type并没有那么简单。
    type的两种作用,一是可以生成类,二是判断一个对象的类型。

    >>> a = 1
    >>> type(1)
    <class 'int'>   # class也是一个对象
    >>> type(int)
    <class 'type'>  # type生成了int,int生成了1                
    
    >>> b = "abc"
    >>> type(b)
    <class 'str'>
    >>> type(str)
    <class 'type'>
    
    """
    wow, 得出来一个现象 type->int->1和type->str->"abc",
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    那么猜测我们自己定义的类是不是也是这样关系呢?

    >>> class Student():
    ...    pass
    ...
    >>> stu = Student()
    >>> type(stu)
    <class '__main__.Student'>
    >>> type(Student)
    <class 'type'>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    soga, 我们可以得出一个结论: type->class->obj,也就是说,我们所定义的类是type类的一个实例化对象,而我们的stu是我们自己定义类Student的一个实例化对象。
    也就是说的是用来生成类的。

    这也是Python一切皆对象的魅力所在

    # object是所有类默认的一个基类, 即object是最顶层基类。
    >>> Student.__bases__
    (<class 'object'>,)
    >>> class MyStudent(Student):
    ...    pass
    ...
    >>> MyStudent.__bases__
    (<class '__main__.Student'>,)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    问题来了:type也是一个类,同时type也是一个对象,那么type的基类是谁?

    '''
    学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441
    寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
    '''
    >>> type.__bases__
    (<class 'object'>,)
    
    # 好玩的来喽,如果执行type(object)看看object类是谁生成的呢?
    >>> type(object)
    <class 'type'>
    >>> type(type)
    <class 'type'>
    
    # 再来好玩的!来看看object类基类谁呢?
    >>> object.__bases__
    ()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    结论:type继承了object,而object又是type的实例,type也是自身的实例。
    type连自己都变成对象,所有类都是type创建出来的(list、str…)。

    object是所有类的基类,type都要继承object。

    总结:

    类都是type的实例 (object也是type的实例,type本身也是自己的实例),所有的类的顶层基类都是object (type的基类也是object)。Python一切皆对象的哲学就是玩起来的。

    Python一切皆对象这么做就是为了一切都可以轻易的修改,使得Python非常灵活。C++/Java中的类一旦加载到内存后就不能修改(不绝对,修改很困难)。Python把类都搞成对象了,这样修改起来就很方便。

    求评论区解释,遇到的问题就是:type需要继承object,但是object又是type的实例,这样的关系有矛盾啊?

    三 、Python中的常见内置类型

    1.对象的三个特征

    (1) 身份

    Copy>>> a = 1
    """
    这里的1是值由进行封装,最后变量a指向这个obj
    """
    >>> id(a)
    5656454878 # 不同机器不同结果
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (2) 类型

    """
    字符串类型
    int类型
    ...
    """
    
    • 1
    • 2
    • 3
    • 4
    • 5

    (3) 值

    2.类型

    (1)None(全局只有一个)

    """
    解释器启动时None对象被创建,且全局只有一个。
    """
    
    >>> a = None
    >>> b = None
    >>> id(a) == id(b)  # 通过对比两个变量所指向内存地址相同,可见None对象全局只有一个。
    True
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (2)数值

    • int

    • float

    • complex (复数)

    • bool

    (3)迭代类型

    • 迭代器
    • 生成器

    (4)序列类型

    • list

    • bytes、bytearray、memoryview (二进制序列)

    • range

    • tuple

    • str

    • array

    (5)映射(dict)

    (6)集合

    • set

    • 与dict实现原理相似,性能高。

    • frozenset

    (7)上下文管理类型(with语句)

    (8)其他

    对于Python,一切皆对象啦。那么就会有以下类型。

    • 模板类型

    • class和实例

    • 函数对象

    • 方法类型

    • 代码类型

    • object类型

    • type 类型

    • ellipsis 类型

    • notimplemented 类型

    Python 的灵活性就使得它的严谨性有一定损失,但是其带给我们开发效率上的提升是显然的。

  • 相关阅读:
    复习mysql中的事务
    Transformer简单理解(MT)
    2023-09-30 关于知识付费的思考与实践
    chatgpt论文润色 & 降重
    Marin说PCB之BGA焊盘削焊盘带来的焊接问题和解决办法
    ArduinoIDE初步开发ESP8266时钟
    传奇出现黑屏卡屏不动是怎么回事
    Docker安装Elasticsearch 8.x 、Kibana 8.x等
    计组笔记——CPU的指令流水
    【ESP32】7.按键实验(中断)
  • 原文地址:https://blog.csdn.net/qdPython/article/details/126972106