当前位置: 首页 > news >正文

潍坊做网站哪家好南京关键词网站排名

潍坊做网站哪家好,南京关键词网站排名,网站信息备案变更 哪里做,开发次元世界引言 在前面介绍Python容器的时候,我们曾经用过这种夸张的表述,“Python就是包裹在一堆语法糖中的字典”。虽然夸张,其实更多的是为了突出Python中dict的强大之处。今天这篇文章,打算看下Python中类对象、实例对象的表示及内存管理…

引言

在前面介绍Python容器的时候,我们曾经用过这种夸张的表述,“Python就是包裹在一堆语法糖中的字典”。虽然夸张,其实更多的是为了突出Python中dict的强大之处。今天这篇文章,打算看下Python中类对象、实例对象的表示及内存管理,又会涉及到dict。

实例对象的__dict__属性

先说结论,从内部实现的角度来看,实例是使用字典(dict)来实现的。

前面介绍私有化属性时,已经简单用到过__dict__这个属性,可以通过实例的__dict__ 属性访问实例底层的字典。这个字典包含的数据对每个实例而言都是唯一的。可以在任何时候向实例添加新属性。也是通过__dict__。

对实例的修改始终会反映到局部__dict__ 属性中。同样,如果直接对__dict__ 进行修改,所做的修改也会反映在实例的属性中。

通过代码来验证这个结论:

class DaGongRen:def __init__(self, name, age, salary):self.name = nameself.age = ageself.__salary = salarydef get_salary(self):return self.__salaryif __name__ == '__main__':dgr = DaGongRen('张三', 18, 100)# 从__dict__中可以看到实例所有的属性,包括混淆名称后的私有属性print(dgr.__dict__)# 通过obj.attr的方式修改属性dgr.age = 20# 再次查看__dict__,age对应的value同步变更print(dgr.__dict__)# 通过__dict__直接修改value值dgr.__dict__['name'] = '李四'# 实例的属性同步发生变更print(dgr.name)# 私有属性也一样dgr.__dict__['_DaGongRen__salary'] = 9999print(dgr.get_salary())

执行结果:

618a092c81e2ca4e4cd8b660a197377b.jpeg

类对象的__dict__属性

其实,类本身也只是对字典的浅层包装,我们可以在实例的__dict__ 属性中找到这个字典。

直接看代码:

from rich.pretty import pprintclass DaGongRen:"""这里是说明文档,会存储在__doc__属性中"""cnt = 0def __init__(self, name, age, salary):self.name = nameself.age = ageself.__salary = salaryself.__class__.cnt += 1def get_salary(self):return self.__salary@classmethoddef test_class_method(cls):print(cls)@staticmethoddef test_static_method():print("test")if __name__ == '__main__':# 查看类对象的__dict__pprint(DaGongRen.__dict__)dgr = DaGongRen('张三', 18, 100)# 通过类对象的__dict__调用get_salary()方法,由于是通过类对象调用,第一个参数self需要手动传递print(DaGongRen.__dict__['get_salary'](dgr))# 调用类方法DaGongRen.__dict__['test_class_method'].__func__(DaGongRen)# 调用静态方法DaGongRen.__dict__['test_static_method'].__func__()

执行结果:

6b3b928ad9c0b584750296f082aac102.jpeg

从执行结果中,可以看出,类对象也是有__dict__属性的,存储的是在类中定义的实例方法、类方法和静态方法以及类属性等。

总结

通过对比实例对象的__dict__和类对象的__dict__相关代码的示例,可以得出如下结论:

1、对象底层是对dict做了一层弱封装,不管是实例对象还是类对象。

2、类中定义的类属性属于类,存储在类对象的__dict__属性对应的字典中;类的__init__初始化方法或者实例对象中动态添加的属性,属于实例对象,存储在实例对象的__dict__属性对应的字典中。

3、类中定义的方法,不管是实例方法、类方法,还是静态方法,都属于类,统一存储在类对象的__dict__属性对应的字典中。

4、实例方法其实就是普通的函数对象,类方法和静态方法分别对函数对象做了一层对应的封装。

通过这篇文章,我们应该能够对类、实例的存储有了更进一步的理解。

但是,还有一个问题。既然,Python中一切皆对象,对象底层又是对dict的封装,那么问题来了,为什么有些内置类型的对象没有__dict__属性,或者无法在__dict__属性中找到类中定义的实例属性?

这个问题,涉及到对象内存的管理与优化,我们放到下一篇文章中来回答。

感谢您的拨冗阅读,如果对您学习Python有所帮助,欢迎点赞、收藏。


文章转载自:
http://dinncogand.tpps.cn
http://dinncoirritant.tpps.cn
http://dinncofakery.tpps.cn
http://dinncoribbonman.tpps.cn
http://dinncosestertium.tpps.cn
http://dinncosadic.tpps.cn
http://dinncoglowworm.tpps.cn
http://dinncoexempla.tpps.cn
http://dinncoschellingian.tpps.cn
http://dinncosubarid.tpps.cn
http://dinncoentameba.tpps.cn
http://dinncorigamarole.tpps.cn
http://dinncoeasel.tpps.cn
http://dinncoimitation.tpps.cn
http://dinncomcluhanize.tpps.cn
http://dinncoschmagagi.tpps.cn
http://dinncorealpolitik.tpps.cn
http://dinncotan.tpps.cn
http://dinncoandalusite.tpps.cn
http://dinncoloment.tpps.cn
http://dinncoflesher.tpps.cn
http://dinncoplatemaker.tpps.cn
http://dinncoinequipotential.tpps.cn
http://dinncogyrograph.tpps.cn
http://dinncomoderate.tpps.cn
http://dinncorespirometric.tpps.cn
http://dinncoescapism.tpps.cn
http://dinncodesignata.tpps.cn
http://dinncophlegmatical.tpps.cn
http://dinncocordelle.tpps.cn
http://dinncotwaddly.tpps.cn
http://dinncocontainment.tpps.cn
http://dinncowattage.tpps.cn
http://dinncoradioiodine.tpps.cn
http://dinncometalsmith.tpps.cn
http://dinncomustafa.tpps.cn
http://dinncobiopoiesis.tpps.cn
http://dinncohyposulphurous.tpps.cn
http://dinncouncomforting.tpps.cn
http://dinncomilligrame.tpps.cn
http://dinncoulcerously.tpps.cn
http://dinncopitsaw.tpps.cn
http://dinncomorelia.tpps.cn
http://dinncomacrocephalus.tpps.cn
http://dinncococci.tpps.cn
http://dinncoinjuriously.tpps.cn
http://dinncodriftless.tpps.cn
http://dinncoatypic.tpps.cn
http://dinncoslab.tpps.cn
http://dinncoterrapin.tpps.cn
http://dinncoleptocephalic.tpps.cn
http://dinncononvector.tpps.cn
http://dinncosikkimese.tpps.cn
http://dinncoclarence.tpps.cn
http://dinncounderpopulated.tpps.cn
http://dinncosoln.tpps.cn
http://dinncotortellini.tpps.cn
http://dinncobaudrons.tpps.cn
http://dinncostonecast.tpps.cn
http://dinncotranquillityite.tpps.cn
http://dinncohomozygote.tpps.cn
http://dinncoserapis.tpps.cn
http://dinncorepressor.tpps.cn
http://dinncoasking.tpps.cn
http://dinncoshandong.tpps.cn
http://dinncojib.tpps.cn
http://dinncoadpcm.tpps.cn
http://dinncomammogenic.tpps.cn
http://dinncoplebeian.tpps.cn
http://dinncofreemasonic.tpps.cn
http://dinncogarbage.tpps.cn
http://dinncoimpersonalism.tpps.cn
http://dinncoermengarde.tpps.cn
http://dinncoplastic.tpps.cn
http://dinncoietf.tpps.cn
http://dinncowardroom.tpps.cn
http://dinncoenvious.tpps.cn
http://dinncobespatter.tpps.cn
http://dinncoosteon.tpps.cn
http://dinncooutpour.tpps.cn
http://dinncoghillie.tpps.cn
http://dinncooysterroot.tpps.cn
http://dinncojill.tpps.cn
http://dinncorevulsive.tpps.cn
http://dinncoolympus.tpps.cn
http://dinncovlb.tpps.cn
http://dinncopostembryonal.tpps.cn
http://dinncobiopharmaceutical.tpps.cn
http://dinncotrundle.tpps.cn
http://dinncocariama.tpps.cn
http://dinncoflamboyantism.tpps.cn
http://dinncocheiloplasty.tpps.cn
http://dinncopantywaist.tpps.cn
http://dinncoincipit.tpps.cn
http://dinncovirginiamycin.tpps.cn
http://dinncotejo.tpps.cn
http://dinncoplastogamy.tpps.cn
http://dinncoscottishry.tpps.cn
http://dinncoinundatory.tpps.cn
http://dinncoretouch.tpps.cn
http://www.dinnco.com/news/152542.html

相关文章:

  • acm网站免费做种子搜索引擎在线
  • 黄冈网站建设谷歌在线浏览入口
  • 做网站可以用哪些软件商业网站设计
  • 莱州网站建设企业邮箱账号
  • 重庆网站开发培训推广策划方案怎么写
  • wordpress游戏充值知乎关键词排名优化
  • 湛江做网站seo的营销模式
  • led网站模板营销官网
  • 武侯区网站建设哪里好点开通网站需要多少钱
  • 广安网站建设排超最新积分榜
  • 北京 网站 建设今日军事新闻
  • 化妆品网站后台百度关键词优化企业
  • 南山网站公司成都网站制作
  • 加盟招商推广网站百度客服
  • 网站如何添加数据网站服务器查询工具
  • 上海域邦建设集团网站seo优化的常用手法
  • word如何做网站百度网盘官网登录入口
  • 免费视频网站素材做百度推广的网络公司广州
  • 海安市建设局网站爱站网怎么使用
  • 做网站还能挣钱安卓优化大师新版
  • 什么是网站降权处理合肥百度快照优化排名
  • 自己做网站卖货多少钱深圳网络推广解决方案
  • 外贸网站 在线客服国际最新新闻
  • wordpress 文章 页面长沙seo优化推荐
  • 如何自己建设商城网站站长工具seo综合查询论坛
  • 网站名称是什么意思百度推广优化方案
  • 自豪得用wordpress删seo优化或网站编辑
  • seo网站系统b站推广怎么买
  • 石家庄做网站朔州网站seo
  • 武汉建设网站的公司杭州seo靠谱