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 开发Activex组件方法
Nov 08 Python
python strip()函数 介绍
May 24 Python
Python中的推导式使用详解
Jun 03 Python
全面了解python字符串和字典
Jul 07 Python
python语音识别实践之百度语音API
Aug 30 Python
python 使用sys.stdin和fileinput读入标准输入的方法
Oct 17 Python
Python爬虫设置代理IP(图文)
Dec 23 Python
Python操作SQLite/MySQL/LMDB数据库的方法
Nov 07 Python
python requests模拟登陆github的实现方法
Dec 26 Python
PyQt5 界面显示无响应的实现
Mar 26 Python
一文搞懂Python Sklearn库使用
Aug 23 Python
python函数的两种嵌套方法使用
Apr 02 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(5) 类和对象
2010/02/16 PHP
php 文件上传类代码
2011/08/06 PHP
解析使用substr截取UTF-8中文字符串出现乱码的问题
2013/06/20 PHP
php实现数据库的增删改查
2017/02/26 PHP
Laravel 创建可以传递参数 Console服务的例子
2019/10/14 PHP
JavaScript中使用正则匹配多条,且获取每条中的分组数据
2010/11/30 Javascript
javascript alert乱码的解决方法
2013/11/05 Javascript
jQuery中append()方法用法实例
2015/01/08 Javascript
JS实现跟随鼠标闪烁转动色块的方法
2015/02/26 Javascript
JavaScript 模块化编程(笔记)
2015/04/08 Javascript
JavaScript中的small()方法使用详解
2015/06/08 Javascript
js仿百度登录页实现拖动窗口效果
2016/03/11 Javascript
JavaScript自动点击链接 防止绕过浏览器访问的方法
2017/01/19 Javascript
JavaScript数据结构之二叉树的计数算法示例
2017/04/13 Javascript
详解如何使用Node.js编写命令工具——以vue-cli为例
2017/06/29 Javascript
JavaScript贪吃蛇小组件实例代码
2017/08/20 Javascript
Angular实现的简单定时器功能示例
2017/12/28 Javascript
原生JS实现轮播图效果
2018/10/12 Javascript
深入理解javascript prototype的相关知识
2019/09/19 Javascript
微信小程序里引入SVG矢量图标的方法
2019/09/20 Javascript
在Python中用split()方法分割字符串的使用介绍
2015/05/20 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
Django对models里的objects的使用详解
2019/08/17 Python
python批量处理txt文件的实例代码
2020/01/13 Python
Django中和时区相关的安全问题详解
2020/10/12 Python
HTML5+Canvas+CSS3实现齐天大圣孙悟空腾云驾雾效果
2016/04/26 HTML / CSS
浅谈HTML5 & CSS3的新交互特性
2016/07/19 HTML / CSS
Oakley官网:运动太阳镜、雪镜和服装
2016/09/30 全球购物
HR喜欢的自荐信格式
2013/10/08 职场文书
大学英语演讲稿范文
2014/04/24 职场文书
汉语专业毕业生自荐信
2014/07/06 职场文书
中学推普周活动总结
2015/05/07 职场文书
爱心捐助活动总结
2015/05/09 职场文书
2015年度个人工作总结报告
2015/10/24 职场文书
css3 选择器
2022/05/11 HTML / CSS
Vue 打包后相对路径的引用问题
2022/06/05 Vue.js