av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)Python教程:python實(shí)例屬性的查找順序

查找順序

1、實(shí)例使用.來訪問屬性,會先找到自己的__dict__。

2、如果沒有,然后通過屬性__class__找到自己的類,再去類的__dict__中找。

注意,如果實(shí)例使用__dict__[變量名]訪問變量,將不會按照上面的查找變量了,這是指明使用字典的key查找,不是屬性查找。一般來說,類變量使用全大寫來命名。

實(shí)例

class Myclass:
    """My class """
    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)  # 50 20 20
 
print(Myclass.heighe,tom.heighe,jerry.heighe)   #   180 180 180
#jerry.heighe = 170
print(Myclass.heighe,tom.heighe,jerry.heighe)   #   180 180 170
 
#tom.heighe +=10
print(Myclass.heighe,tom.heighe,jerry.heighe)   #   180 190 180
 
#Myclass.heighe += 20
print(Myclass.heighe,tom.heighe,jerry.heighe)   #   200 200 200
 
Myclass.weight = 90
print(Myclass.weight,tom.weight,jerry.weight)  #    90 90 90
 
print(Myclass.__dict__["age"])  #   18
print(jerry.__dict__["age"])    #   20
print(tom.__dict__["heighe"])   #   KeyError: 'heighe'
print(Myclass.__dict__["weight"])   #   90

以上就是Python實(shí)例屬性的查找順序,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。


分享題目:創(chuàng)新互聯(lián)Python教程:python實(shí)例屬性的查找順序
轉(zhuǎn)載來于:http://uogjgqi.cn/article/dhoicis.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流