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中的装饰器、闭包和functools的教程
Apr 02 Python
简单介绍Python下自己编写web框架的一些要点
Apr 29 Python
Python中isnumeric()方法的使用简介
May 19 Python
python中requests库session对象的妙用详解
Oct 30 Python
Python定时器实例代码
Nov 01 Python
python切片及sys.argv[]用法详解
May 25 Python
python实现淘宝秒杀脚本
Jun 23 Python
Python告诉你木马程序的键盘记录原理
Feb 02 Python
eclipse创建python项目步骤详解
May 10 Python
Python 硬币兑换问题
Jul 29 Python
如何通过python实现IOU计算代码实例
Nov 02 Python
解决pycharm修改代码后第一次运行不生效的问题
Feb 06 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
Codeigniter购物车类不能添加中文的解决方法
2014/11/29 PHP
Laravel实现表单提交
2017/05/07 PHP
php合并数组并保留键值的实现方法
2018/03/12 PHP
jQuery使用手册之一
2007/03/24 Javascript
event对象的方法 兼容多浏览器
2009/06/27 Javascript
jQuery代码优化 遍历篇
2011/11/01 Javascript
js操作textarea 常用方法总结
2012/12/03 Javascript
jquery实现经典的淡入淡出选项卡效果代码
2015/09/22 Javascript
详解数组Array.sort()排序的方法
2020/05/09 Javascript
原生js实现简单的链式操作
2017/07/04 Javascript
js使用原型对象(prototype)需要注意的地方
2017/08/28 Javascript
JS switch判断 三目运算 while 及 属性操作代码
2017/09/03 Javascript
Javascript刷新页面的实例
2017/09/23 Javascript
浅谈Vue的加载顺序探讨
2017/10/25 Javascript
js 获取本周、上周、本月、上月、本季度、上季度的开始结束日期
2020/02/01 Javascript
vuecli项目构建SSR服务端渲染的实现
2020/10/30 Javascript
Python中对元组和列表按条件进行排序的方法示例
2015/11/10 Python
如何用itertools解决无序排列组合的问题
2017/05/18 Python
使用Python爬取最好大学网大学排名
2018/02/24 Python
python 将md5转为16字节的方法
2018/05/29 Python
Python使用requests提交HTTP表单的方法
2018/12/26 Python
Python3之不使用第三方变量,实现交换两个变量的值
2019/06/26 Python
python 图片去噪的方法示例
2019/07/09 Python
使用python计算三角形的斜边例子
2020/04/15 Python
如何用Python和JS实现的Web SSH工具
2021/02/23 Python
CSS3标注引用的出处和来源的方法
2020/02/25 HTML / CSS
美国最大的家庭鞋类零售商之一:Shoe Carnival
2017/10/06 全球购物
旅游管理专业学生求职信
2013/09/28 职场文书
运动会获奖感言
2014/02/11 职场文书
置业顾问岗位职责
2014/03/02 职场文书
报关员个人职业生涯规划书
2014/03/12 职场文书
交通文明倡议书
2014/05/16 职场文书
微笑面对生活演讲稿
2014/09/23 职场文书
培根随笔读书笔记
2015/07/01 职场文书
小学体育课教学反思
2016/02/16 职场文书
vue3获取当前路由地址
2022/02/18 Vue.js