首页IT科技python123查找指定字符(python实例属性的查找顺序)

python123查找指定字符(python实例属性的查找顺序)

时间2025-06-13 18:10:42分类IT科技浏览3651
导读:查找顺序...

查找顺序

1             、实例使用.来访问属性             ,会先找到自己的__dict__             。

2                    、如果没有                    ,然后通过属性__class__找到自己的类       ,再去类的__dict__中找                    。

注意             ,如果实例使用__dict__[变量名]访问变量                   ,将不会按照上面的查找变量了       ,这是指明使用字典的key查找       ,不是属性查找       。一般来说                   ,类变量使用全大写来命名      。

实例

classMyclass: """Myclass""" heighe=180 age=18 def__init__(self,name,age=20): self.name=name self.age=age jerry=Myclass("jerry",20) tom=Myclass("tom") #Myclass.age=50 print(Myclass.age,tom.age,jerry.age)#502020 print(Myclass.heighe,tom.heighe,jerry.heighe)#180180180 #jerry.heighe=170 print(Myclass.heighe,tom.heighe,jerry.heighe)#180180170 #tom.heighe+=10 print(Myclass.heighe,tom.heighe,jerry.heighe)#180190180 #Myclass.heighe+=20 print(Myclass.heighe,tom.heighe,jerry.heighe)#200200200 Myclass.weight=90 print(Myclass.weight,tom.weight,jerry.weight)#909090 print(Myclass.__dict__["age"])#18 print(jerry.__dict__["age"])#20 print(tom.__dict__["heighe"])#KeyError:heighe print(Myclass.__dict__["weight"])#90

以上就是python实例属性的查找顺序             ,希望对大家有所帮助                    。更多Python学习指路:Python基础教程

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
yolov5数据集(YOLOv7教程系列:一、基于自定义数据集训练专属于自己的目标检测模型(保姆级教程,含数据集预处理),包含对train.py/test.py/detect.py/export.py详细说明) vuecli自定义组件(Vue3自动引入组件,组件库)