python第七周學習內容
1.反射:
1.1定義:通過字符串映射或修改程序運行時的狀態、屬性、方法
1.2有以下四個方法:
(1)hasattr(object,str) 判斷object對象中是否有對應的方法或屬性,返回值:True·、False
class People(object): ‘‘‘this is the description of People‘‘‘ def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age def eat(self):print("%s is eating...."%self.name) def piao(self): print("%s is piaoing..."%self.name) def __call__(self, *args, **kwargs): print("My name is Mr Wu") def __str__(self): return "hello world" man = People("dog","male",19) if hasattr(man,"name") and hasattr(man,"eat"): print("hello world!") #output:hello world!
(2)func = getattr(object,str)根據str去獲取object對象中的對應的方法的內存地址或屬性的值(調用:func(argument))
class People(object): ‘‘‘this is the description of People‘‘‘ def __init__(self,name,sex,age): self.name = name self.sex = sex self.age= age def eat(self): print("%s is eating...."%self.name) def piao(self): print("%s is piaoing..."%self.name) def __call__(self, *args, **kwargs): print("My name is Mr Wu") def __str__(self): return "hello world" man = People("dog","male",19) func = getattr(man,"eat") func() #dog is eating.... name = getattr(man,"name") print(name) #dog
(3)delattr(object,str) 刪除object對象中的str對應的方法或屬性
class People(object): ‘‘‘this is the description of People‘‘‘ def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age def eat(self): print("%s is eating...."%self.name) def piao(self): print("%s is piaoing..."%self.name) def __call__(self, *args, **kwargs): print("My name is Mr Wu") def __str__(self): return "hello world" man = People("dog","male",19) delattr(man,"name") print(man.name) #AttributeError: ‘People‘ object has no attribute ‘name‘ delattr(man,"eat") man.eat()#AttributeError: eat
(4)setattr(x,y,v)相當於x.y = v,設置新的屬性或方法(可修改原有的屬性和方法)
class People(object): ‘‘‘this is the description of People‘‘‘ def __init__(self,name,sex,age): self.name = name self.sex = sex self.age = age def eat(self): print("%s is eating...."%self.name) def piao(self): print("%s is piaoing..."%self.name) def sleep(self): print("%s is sleeping...."%(self.name)) name = "Mr Wu" man = People("dog","male",19) setattr(man,"sleep",sleep) man.sleep(man) #dog is sleeping.... setattr(man,"name",name) print(man.name) #Mr Wu
2.異常處理
2.1 概述:
Python的異常處理能力是很強大的,可向用戶準確反饋出錯信息。在Python中,異常也是對象,可對它進行操作。所有異常都是基類Exception的成員。所有異常都從基類Exception繼承,而且都在exceptions模塊中定義。Python自動將所有異常名稱放在內建命名空間中,所以程序不必導入exceptions模塊即可使用異常。一旦引發而且沒有捕捉SystemExit異常,程序執行就會終止。如果交互式會話遇到一個未被捕捉的SystemExit異常,會話就會終止。
2.2 python中所有的標準異常類
2.2 語法格式
try:
代碼塊
except 異常名稱 as e:
print(e) #輸出異常的內容
except 異常名稱 as e:
print(e) #輸出異常處理
...............
特別地:
except Exception as e:
#如果不知道異常具體是什麽類型,可以使用Exception
else:
#如果沒有出現前面的異常則執行這條語句
finally:
#不管有沒有出現前面的異常都會執行這條語句
註:如果已經捕獲到了try下的一條異常,那麽就不會再捕獲其他異常了
代碼實例:
names = ["Mr Wu","Ms Li"] dict_names = {"name":"Mr Wu","age":19} try: print(names[3]) dict_names["Mr Wang"] except IndexError as e: print("出錯了:",e) except KeyError as e: print("出錯了:",e) except Exception as e: print("未知錯誤:",e) #output:出錯了: list index out of range
names = ["Mr Wu","Ms Li"] dict_names = {"name":"Mr Wu","age":19} try: print(names[3]) dict_names["Mr Wang"] #except IndexError as e: # print("出錯了:",e) #except KeyError as e: # print("出錯了:",e) except Exception as e: print("未知錯誤:",e) #output:未知錯誤: list index out of range
2.3 自定義異常
我們可以自己定義異常類,並且觸發執行這個異常。
註:自定義的異常必須繼承異常類基類Exception,並通過raise語句實例化這個類
class AlexException(Exception): def __init__(self,msg): self.message = msg try: raise AlexException("\033[1;41m數據庫連接失敗\033[0m") except AlexException as e: print(e) #output:數據庫連接失敗
python第七周學習內容