類屬性與對象屬性的增刪改查
類屬性的增刪改查html
class School: """ 文檔 """ Teacher = "老王"
def __init__(self,name):
self.name = name def Examination(self): print("%s的班級正在考試"%self) p1 = School("小李") '''類數據屬性的增刪改查 查 print(School.Teacher) 改 School.Teacher="老李" print(School.Teacher) 增 School.foo ="小孫" print(School.__dict__) 刪 del School.foo print(School.__dict__) ''' #類函數屬性的增刪改查同實例屬性 def eat_food(self,food): print("正在吃") School.eat = eat_food print(School.__dict__) School.eat
對象(實例)屬性的增刪改查python
class Chinese: country = "china" def __init__(self,name): self.mingzi = name def play_ball(self,ball): print("%s 正在打 %s"%(self.mingzi,ball)) p1 = Chinese("alex") print(p1.__dict__) #查 print(p1.mingzi) p1.play_ball("籃球")#其實是調用類的函數屬性 #增 p1.age = 18 print(p1.__dict__) #改 p1.age = 29 print(p1.__dict__) #刪 del p1.age print(p1.__dict__)
小結
注意:點的方式調用要麼跟類有關,要麼跟實例有關,不加點調用就是普通變量app
#點的方式調用要麼跟類有關,要麼跟實例有關,不加點調用就是普通變量 country = "中國" class Chinese: country = "china" l = ["a","b"] def __init__(self,name): self.mingzi = name def play_ball(self,ball): print("%s 正在打 %s"%(self.mingzi,ball)) p1 = Chinese("alex") p1.country = "老王" #屬於給實例化新增一個字典,不影響類的字典 print(Chinese.country) print(p1.country) p1.l.append("c") #屬於調用類的屬性,與實例化無關 print(Chinese.l) print(p1.l) print(p1.__dict__)#實例化字典中沒有l