介绍
这篇文章给大家分享的是有关python实例属性的查找顺序是什么的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
python是什么意思
python是一种跨平台的,具有解释性,编译性,互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。
<强> 1,查找顺序强>
指的是实例使用。来访问属性,会先找到自己的__dict__,如果没有,然后通过属性__class__进行找到自己的类,再去类的__dict__中找。
注意,如果实例使用__dict__[变量名]访问变量,将不会按照上面的查找变量了,这是指明使用字典的关键查找,不是属性查找。一般来说,类变量使用全大写来命名。
<强> 2,实例强>
class Myclass: ,,,“““My class “““ ,,,heighe =180 ,,,age =18 ,,,def __init__(自我、名字、年龄=20): ,,,,,,,self.name =,名字 ,,,,,,,self.age =,年龄 , , , 时间=jerry Myclass (“jerry", 20) 时间=tom Myclass (“tom") , # Myclass.age =50 打印(Myclass.age tom.age jerry.age),, #, 50, 20, 20 , 打印(Myclass.heighe tom.heighe jerry.heighe),,, #,,, 180, 180, 180 # jerry.heighe =170 打印(Myclass.heighe tom.heighe jerry.heighe),,, #,,, 180, 180, 170 , # tom.heighe +=10 打印(Myclass.heighe tom.heighe jerry.heighe),,, #,,, 180, 190, 180 , # Myclass.heighe +=20 打印(Myclass.heighe tom.heighe jerry.heighe),,, #,,, 200, 200, 200 , Myclass.weight =90 打印(Myclass.weight tom.weight jerry.weight),, #,,,, 90, 90, 90 , print (Myclass.__dict__ [“age"]),, #,,, 18岁 print (jerry.__dict__ [“age"]),,,, #,, 20 print (tom.__dict__ [“heighe"]),,, #,,, KeyError:, & # 39; heighe& # 39; print (Myclass.__dict__ [“weight"]),,, #,,, 90年
感谢各位的阅读!关于“python实例属性的查找顺序是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!