# Python 類別&裝飾器 學習筆記 ###### tags: `python` Copyright 2021, [月下麒麟](https://hackmd.io/@YMont/note-catalog) --- 此篇僅作紀錄用~ reference:[[Python教學] Class / Static /Abstract Method 初探](https://www.maxlist.xyz/2019/12/08/python-class-static-abstract-method/) 因該原作寫得相當清楚,而做為自我練習並引用其程式當範例 **定義屬性的三個方法** ```python= class ThisTestClass: getName = 'Max' #方法一:把屬性定義寫在init外面 def __init__(self): self.getNameFromInit = ‘Max__init__’ #方法二:把屬性定義寫在init內 pass if __name__ == “__main__”: task = ThisTestClass() task.sendName = ‘Max_sendName’ ##方法三:自己給屬性與參數 print(task.getName) print(task.getNameFromInit) print(task.sendName) >>> Max >>> Max__init__ >>> Max_sendName ``` --- ## staticmethod classmethod * Static method 靜態方法:不帶實例,不帶 class 為參數的方法 * Class method 類方法:不帶實例,帶有 class 為參數的方法 * Abstract method 抽象方法:尚未被實作,且繼承 class 一定要用覆寫來實作 **Staticmethod** * StaticMethods 使用方法: 在 def 函式上加上 @staticmethod,不用傳入 self 參數 * StaticMethods 使用時機: ==不在需要將 class 實例後才能使用函式,直接像以下範例呼叫 People_StaticMethods.work(4) 即可使用== ```python= class People_StaticMethods: def __init__(self): pass def sleep(self, sleep_hour): print('Sleeping hours :', sleep_hour) @staticmethod def work(work_hour): print('Working hours :', work_hour) m = People_StaticMethods() m.sleep(3) People_StaticMethods.work(4) # >>> Sleeping hours : 3 # >>> Working hours : 4 ``` --- **classmethod** * ClassMethods 使用方法: 在 def 函式上加上 @classmethod 必須傳入 class 本身參數,通常大家都會命名為 cls 如果要引入 class 其他函式,可以使用 cls().sleep(6) * ClassMethods 使用時機: ==不在需要將 class 實例後才能使用函式,直接像以下範例呼叫 People_ClassMethods.work(5) 即可使用 不同於 StaticMethods,因為多引入了 class 本身參數為 cls,可以利用 cls 來 access 其他 class 內的函式== ```python= class People_ClassMethods: def __init__(self): pass def sleep(self, sleep_hour): print('Sleeping hours :', sleep_hour) @classmethod def work(cls, work_hour): print('Working hours :', work_hour) cls().sleep(6) People_ClassMethods.work(5) ``` --- **AbstractMethod** * AbstractMethods 使用時機: 抽象類 (Employee) 的特點是不能實例化,只能被子類繼承 任何繼承 Employee 的類,都必須有實現 work 方法,否則會拋出異常,像是 class Max(Employee) 中,只定義了 def sleep(self),沒有定義 work 方法,所以再引用時會出現 Can’t instantiate abstractclass Max with abstract methods work 的錯誤。 ```python= import abc class Employee(abc.ABC): @abc.abstractmethod def work(self): return NotImplemented class Andy(Employee): def work(self): print('work') class Max(Employee): def sleep(self): print('sleep') Andy().work() # >>> work Max().sleep() # >>> Traceback (most recent call last): # File "/Users/max/Desktop/python_learning/methods.py", line 77, in <module> Max().sleep() TypeError: Can't instantiate abstractclass Max with abstract methods work ``` --- ## 裝飾器 ```python= from functools import wraps import time def timer(func): @wraps(func) def wrap(): t_start = time.time() print("1") func() print("8") t_end = time.time() t_count = t_end - t_start print("[花費時間]", t_count) return wrap def func_print_one(func): @wraps(func) def wrap(): print('2') func() print("7") return wrap def func_print_two(func): @wraps(func) def wrap(): print('3') func() print("6") return wrap @timer @func_print_one @func_print_two def dosomething(): print("4") print('do some thing') print("5") dosomething() # ----- # >>> 1 # >>> 2 # >>> 3 # >>> 4 # >>> do some thing # >>> 5 # >>> 6 # >>> 7 # >>> 8 # >>> [花費時間] 0.049613237380981445 ``` 我的理解是,裝飾器由上而下 先去裝飾器裡面 再到使用裝飾器的函式 最後,再回到裝飾器執行完畢 遇到兩層或以上 一樣由上而下