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统计文件行数示例分享
Feb 21 Python
Python中类的定义、继承及使用对象实例详解
Apr 30 Python
python实现简单登陆流程的方法
Apr 22 Python
Python3读取Excel数据存入MySQL的方法
May 04 Python
用python标准库difflib比较两份文件的异同详解
Nov 16 Python
对python判断ip是否可达的实例详解
Jan 31 Python
在linux下实现 python 监控usb设备信号
Jul 03 Python
面向对象学习之pygame坦克大战
Sep 11 Python
Python 捕获代码中所有异常的方法
Aug 03 Python
python 装饰器的实际作用有哪些
Sep 07 Python
Pytorch 扩展Tensor维度、压缩Tensor维度的方法
Sep 09 Python
python 使用openpyxl读取excel数据
Feb 18 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以及MYSQL日期比较方法
2012/11/29 PHP
使用PHP实现Mysql读写分离
2013/06/28 PHP
php读取mysql的简单实例
2014/01/15 PHP
php跨域cookie共享使用方法
2014/02/20 PHP
PHP函数eval()介绍和使用示例
2014/08/20 PHP
php网站被挂木马后的修复方法总结
2014/11/06 PHP
php ajax异步读取rss文档数据
2016/03/29 PHP
php实现数据库的增删改查
2017/02/26 PHP
PHP实现数组转JSon和JSon转数组的方法示例
2018/06/14 PHP
PHP调用微博接口实现微博登录的方法示例
2018/09/22 PHP
倾力总结40条常见的移动端Web页面问题解决方案
2016/05/24 Javascript
Vue.js父与子组件之间传参示例
2017/02/28 Javascript
利用jQuery解析获取JSON数据
2017/04/08 jQuery
详解vue嵌套路由-params传递参数
2017/05/23 Javascript
js实现图片懒加载效果
2017/07/17 Javascript
详解为Bootstrap Modal添加拖拽的方法
2018/01/05 Javascript
详解微信小程序开发用户授权登陆
2019/04/24 Javascript
详解vue 图片上传功能
2019/04/30 Javascript
jQuery实现小火箭返回顶部特效
2020/02/03 jQuery
[01:32]2014DOTA2西雅图邀请赛 CIS我们有信心进入正赛
2014/07/08 DOTA
python使用内存zipfile对象在内存中打包文件示例
2014/04/30 Python
零基础写python爬虫之抓取百度贴吧并存储到本地txt文件改进版
2014/11/06 Python
Scrapy-redis爬虫分布式爬取的分析和实现
2017/02/07 Python
Python中的类与类型示例详解
2019/07/10 Python
Python中的相关分析correlation analysis的实现
2019/08/29 Python
python程序 线程队列queue使用方法解析
2019/09/23 Python
python实现LRU热点缓存及原理
2019/10/29 Python
TensorFlow通过文件名/文件夹名获取标签,并加入队列的实现
2020/02/17 Python
Marc Jacobs官方网站:美国奢侈品牌
2017/08/29 全球购物
阿姆斯特丹城市卡:Amsterdam Pass
2019/12/01 全球购物
网络工程师的自我评价
2013/10/02 职场文书
总经理的岗位职责
2014/02/23 职场文书
给学校的建议书
2014/03/12 职场文书
大学教师个人总结
2015/02/10 职场文书
2015清明节祭奠英烈寄语大全
2015/03/04 职场文书
2015年四年级班主任工作总结
2015/10/22 职场文书