Python类和实例的属性机制原理详解


Posted in Python onMarch 21, 2020

实例是具象化的类,它可以作为类访问所有静态绑定到类上的属性,包括类变量与方法,也可以作为实例访问动态绑定到实例上的属性。

实例1:

class A:
  work = list("hello")
  kind = list("world")
  another = 1

  def test1(self):
    print(self.work, self.kind, self.another)
    self.work[0], self.kind [0] = "t", "t"
    self.another += 1
    print(A.work, A.kind, A.another)
if __name__ == "__main__":
  a = A()
  a.test1()

输出结果:

['h', 'e', 'l', 'l', 'o'] ['w', 'o', 'r', 'l', 'd'] 1
['t', 'e', 'l', 'l', 'o'] ['t', 'o', 'r', 'l', 'd'] 1

test1中演示了实例对类变量的访问与修改,从输出结果可以看到,类变量work和kind的列表被修改了,而another的值没有发生变化,说明如果类变量是可变的,那么可以通过实例来对类变量进行修改,如果类变量不可变,那么实例无法修改类变量。

实例2:

class A:
  work = list("hello")
  kind = list("world")
  another = 1

  def test2(self):
    A.work, A.kind = "hello", " world"
    A.another += 2
    print(self.__dict__)
    print(self.work, self.kind, self.another)
    A.test2 = 13
    print(self.test2)
if __name__ == "__main__":
  a = A()
  a.test2()

输出结果:

 {'another': 2}
 hello world 2
 13

test2说明了实例访问类变量与方法的机制,在test1中,已经给实例动态绑定了一个another的属性,值为2(因为有赋值语句)。在self.__dict__中可以看到确实出现了实例属性another。

在使用实例访问属性(变量与方法)时,如果在实例的属性集里没有找到对应的属性,那么就会到类的属性集里找对应的属性。self.work和self.kind和类变量保持一致,说明并没有事先在实例与类变量之间建立引用,而是动态查找的。

class A:
  work = list("hello")
  kind = list("world")
  another = 1

  def test3(self):
    print(self.__dict__)
    self.w, self.k = 0, 1
    print(self.__dict__)
    self.work, self.kind = 4, 4
    print(self.__dict__)
    self.test1 = 12
    print(self.__dict__)
    try:
      self.test1()
    except:
      print("test1 is not a bound method")
if __name__ == "__main__":
  a = A()
  a.test3()

输出结果:

 {'another': 2}
 {'another': 2, 'w': 0, 'k': 1}
 {'another': 2, 'w': 0, 'k': 1, 'work': 4, 'kind': 4}
 {'another': 2, 'w': 0, 'k': 1, 'work': 4, 'kind': 4, 'test1': 12}
 test1 is not a bound method

self.__dict__中保存了动态绑定到实例的变量与方法,只要出现了赋值语句,都是动态绑定属性。如果动态绑定的属性与类的变量或方法同名,在查找过程中就会覆盖类的变量和方法。

总结

1. 动态绑定到实例的属性位于self.__dict__中

2. 出现self.attribute = XXX之类的赋值语句都是在往实例上动态绑定属性

3. 实例查找属性的流程:self.work -> self.__dict__["work"] or cls.work,这是一个动态的过程,实例中的同名属性会覆盖类变量或方法,类变量或方法的修改会实时影响实例查找属性的结果

4. 如果类变量是可修改的,如列表,字典等,可以通过实例来修改类变量,方法是不可修改的,故无法通过实例修改方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python单链表简单实现代码
Apr 27 Python
Python实现求笛卡尔乘积的方法
Sep 16 Python
python得到windows自启动列表的方法
Oct 14 Python
python中的数据结构比较
May 13 Python
Python使用scipy模块实现一维卷积运算示例
Sep 05 Python
python通过SSH登陆linux并操作的实现
Oct 10 Python
python双端队列原理、实现与使用方法分析
Nov 27 Python
Win下PyInstaller 安装和使用教程
Dec 25 Python
python 成功引入包但无法正常调用的解决
Mar 09 Python
Python 调用 ES、Solr、Phoenix的示例代码
Nov 23 Python
python绘制高斯曲线
Feb 19 Python
python数据分析之单因素分析线性拟合及地理编码
Jun 25 Python
Python生成器常见问题及解决方案
Mar 21 #Python
Python作用域与名字空间原理详解
Mar 21 #Python
Python小整数对象池和字符串intern实例解析
Mar 21 #Python
Python描述符descriptor使用原理解析
Mar 21 #Python
Python如何省略括号方法详解
Mar 21 #Python
Python如何使用bokeh包和geojson数据绘制地图
Mar 21 #Python
Spring Boot中使用IntelliJ IDEA插件EasyCode一键生成代码详细方法
Mar 20 #Python
You might like
php编程每天必学之验证码
2016/03/03 PHP
php微信公众平台开发(一) 配置接口
2016/12/06 PHP
php中html_entity_decode实现HTML实体转义
2018/06/13 PHP
提高Laravel应用性能方法详解
2019/06/24 PHP
PHP pthreads v3在centos7平台下的安装与配置操作方法
2020/02/21 PHP
在Javascript中定义对象类别
2006/12/22 Javascript
使用node.js 获取客户端信息代码分享
2014/11/26 Javascript
js获取当前周、上一周、下一周日期
2017/03/19 Javascript
vue mint-ui 实现省市区街道4级联动示例(仿淘宝京东收货地址4级联动)
2017/10/16 Javascript
在vue中添加Echarts图表的基本使用教程
2017/11/22 Javascript
JS简单添加元素新节点的方法示例
2018/02/10 Javascript
详解webpack-dev-server 设置反向代理解决跨域问题
2018/04/18 Javascript
vue 使用自定义指令实现表单校验的方法
2018/08/28 Javascript
vue2中引用及使用 better-scroll的方法详解
2018/11/15 Javascript
Vue CLI项目 axios模块前后端交互的使用(类似ajax提交)
2019/09/01 Javascript
JQuery复选框全选效果如何实现
2020/05/08 jQuery
vue中配置scss全局变量的步骤
2020/12/28 Vue.js
Python中条件选择和循环语句使用方法介绍
2013/03/13 Python
Python入门之三角函数atan2()函数详解
2017/11/08 Python
python selenium 执行完毕关闭chromedriver进程示例
2019/11/15 Python
Python打包模块wheel的使用方法与将python包发布到PyPI的方法详解
2020/02/12 Python
python实现全排列代码(回溯、深度优先搜索)
2020/02/26 Python
canvas仿写贝塞尔曲线的示例代码
2017/12/29 HTML / CSS
Born鞋子官网:Born Shoes
2017/04/06 全球购物
小女主人连衣裙:Little Mistress
2017/07/10 全球购物
80年代复古T恤:TruffleShuffle
2018/07/02 全球购物
电子商务应届生求职信
2013/11/16 职场文书
社会实践感言
2014/01/25 职场文书
大学应届毕业生求职信
2014/05/24 职场文书
校园文化标语
2014/06/18 职场文书
工作粗心大意检讨书
2014/09/18 职场文书
党的群众路线学习笔记
2014/11/06 职场文书
感谢信模板大全
2015/01/23 职场文书
2016年春季运动会加油稿
2015/07/22 职场文书
mysql 如何获取两个集合的交集/差集/并集
2021/06/08 MySQL
vue @ ~ 相对路径 路径别名设置方式
2022/06/05 Vue.js