zoukankan      html  css  js  c++  java
  • 【Python核心编程笔记】一、Python中一切皆对象

    Python中一切皆对象

    本章节首先对比静态语言以及动态语言,然后介绍 python 中最底层也是面向对象最重要的几个概念-object、type和class之间的关系,以此来引出在python如何做到一切皆对象、随后列举python中的常见对象。

    1.Python中一切皆对象

    Python的面向对象更彻底,Java和C++中基础类型并不是对象。在Python中,函数和类也是对象,属于Python的一等公民。对象具有如下4个特征

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

    下面从四个特征角度分别举例说明函数和类也是对象

    1.1 类和函数都可以赋值给一个变量

    类可以赋值给一个变量

    class Person:
        def __init__(self, name="lsg"):
            print(name)
    
    
    if __name__ == '__main__':
        my_class = Person  # 类赋值给一个变量
        my_class()  # 输出lsg,变量直接调用就可以实例化一个类,满足上面的特征1,这里显然说明类也是一个对象
        my_class("haha")  # 输出haha
    
    

    函数可以赋值给一个变量

    def func_test(name='lsg'):
        print(name)
    
    
    if __name__ == '__main__':
        my_func = func_test
        my_func("haha") # 输出haha,对变量的操作就是对函数的操作,等效于对象的赋值,满足上面的特征1,说明函数是对象。
    

    1.2 类和函数都可以添加到集合中

    class Person:
        def __init__(self, name="lsg"):
            print(name)
    
    
    def func_test(name='lsg'):
        print(name)
    
    
    if __name__ == '__main__':
        obj_list = [func_test, Person]
        print(obj_list) # 输出[<function func_test at 0x0000025856A2C1E0>, <class '__main__.Person'>]
    

    1.3 类和函数都可以作为参数传递给函数

    class Person:
        def __init__(self, name="lsg"):
            print(name)
    
    
    def func_test(name='lsg'):
        print(name)
    
    
    def print_type(obj):
        print(type(obj))
    
    
    if __name__ == '__main__':
        print_type(func_test)
        print_type(Person)
    

    输出如下

    <class 'function'>
    <class 'type'>
    

    可以明显地看出类和函数都是对象

    1.4 类和函数都可以作为函数地返回值

    class Person:
        def __init__(self, name="lsg"):
            print(name)
    
    
    def func_test(name='lsg'):
        print(name)
    
    
    def decorator_func():
        print("pre function")
        return func_test
    
    
    def decorator_class():
        print("pre class")
        return Person
    
    
    if __name__ == '__main__':
        decorator_func()()  # 返回的右值作为函数可以直接调用
        decorator_class()()  # 返回的右值作为类可以直接实例化
    

    2.type、object和class的关系

    代码举例如下, 可以得出三者的关系是type --> class --> obj

    2.1 type --> int --> a

    a = 1
    print(type(a)) # <class 'int'>
    print(type(int)) # <class 'type'>
    

    2.2 type --> str --> b

    b = 'abc'
    print(type(b)) # <class 'str'>
    print(type(str)) # <class 'type'>
    

    2.3 type --> Student --> stu

    class Student:
        pass
    
    stu = Student()
    print(type(stu)) # <class '__main__.Student'>
    print(type(Student)) # <class 'type'>
    

    2.4 type --> list --> c

    c = [1, 2]
    print(type(c)) # <class 'list'>
    print(type(list)) # <class 'type'>
    

    总结图:
    file

    3.Python中常见的内置类型

    对象的三个特征:身份、内存和值

    • 身份:在内存中的地址,可以用id(变量)函数来查看
    • 类型:任何变量都必须有类型

    常见的内置类型如下

    3.1 None:全局只有一个

    如下代码,两个值为None的变量地址完全相同,可见None是全局唯一的

    a = None
    b = None
    print(id(a))
    print(id(b))
    print(id(a) == id(b))
    

    3.2 数值类型

    • int
    • float
    • complex(复数)
    • bool

    3.3 迭代类型:iterator

    3.4 序列类型

    • list
    • bytes、bytearray、memoryview(二进制序列)
    • range
    • tuple
    • str
    • array

    3.5 映射类型(dict)

    3.6 集合

    • set
    • frozenset

    3.7 上下文管理类型(with)

    3.8 其他

    • 模块类型
    • class和实例
    • 函数类型
    • 方法类型
    • 代码类型
    • object类型
    • type类型
    • elipsis类型
    • notimplemented类型

    欢迎关注我的公众号查看更多文章

    空无一悟

  • 相关阅读:
    《C程序设计语言现代方法》第5章 选择语句
    《C语言程序设计现代方法》第4章 编程题
    《C语言程序设计现代方法》第4章 表达式
    《算法竞赛入门经典》第1章 程序设计入门
    《C语言程序设计现代方法》第3章 格式化输入/输出
    《C语言程序设计现代方法》第2章 编程题
    《C语言程序设计现代方法》第2章 C语言基本概念
    《C语言程序设计现代方法》第1章 C语言概述
    Linux和Windows下的进程管理总结
    silvetlight ListBox Item项自动填满
  • 原文地址:https://www.cnblogs.com/lsgwr/p/14414121.html
Copyright © 2011-2022 走看看