python 一切皆对象的体现

python 一切皆对象

哪些是对象

  1. bulit-in types是对象
    string、digits、list、tuple、dict对应的变量
  2. 类是对象
  3. 函数是对象

认识一个对象:

  1. 可以赋值给一个变量
  2. 可以对其属性进行操作
    “getattr”, “setattr” and “delattr”. 3. 可以当做是函数的参数
  3. 一些方法
    isinstance(everything,object)是否是对象
    vars() 包含变量
    dir(everything) 可用方法
    help(everything) 说明文档
    everything.__doc__说明文档

第一等对象(first-class objects)

四个特征:

  • 运行时被创建:Created at runtime
  • 可赋值给变量:Assigned to a variable or element in a data structure
  • 可传参给函数:Passed as an argument to a function
  • 可作为函数的返回值:Returned as the result of a functio

函数作为第一等对象的操作:刷新三观!

  1. 函数赋值给变量、也可以存在数据结构中
  2. 外函数调用内函数
    • 函数闭包closure
      变量的作用域的寻找方向如下:
      LEGB:local\enclosing\global\built-in
      函数:global\local\nonlocal
  3. 将函数当成参数传入
  4. 可以return一个函数

以刚刚的类为例,对对象的属性进行操作:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43899514/article/details/109387523