兩個裝飾器python
classmethod : 被裝飾的方法會成爲一個靜態方法mysql
classmethod 何時用?linux
定義:sql
裝飾器怎麼加設計模式
參數怎麼改app
class Goodds: __dicount = 0.8 def __init__(self): self.__Price = 5 self.price = self.__Price * self.__dicount @classmethod def change_discount(cls, new_discount): cls.__dicount = new_discount
用法:函數
調用方法設計
class Goodds: __dicount = 0.8 def __init__(self): self.__Price = 5 self.price = self.__Price * self.__dicount @classmethod def change_discount(cls, new_discount): cls.__dicount = new_discount # 類方法能夠經過類名調用 Goodds.change_discount(0.6) apple = Goodds() print(apple.price) # 類方法能夠經過對象名調用 apple.change_discount(0.5) apple2 = Goodds() print(apple2.price) # import time # class Date: # def __init__(self, year, month, day): # self.year = year # self.month = month # self.day = day # # @classmethod # def today(cls): # struct_t = time.localtime() # date = cls(struct_t.tm_year, struct_t.tm_mon, struct_t.tm_mday) # return date # # # date_obj = Date.today() # print(date_obj.year) # print(date_obj.month) # print(date_obj.day) # # 2019 # # 6 # # 5
staticmethod : 被裝飾的方法會成爲一個靜態方法指針
用在:code
幫助咱們把一個普通的函數挪到類中來直接使用,製造靜態方法用的
定義:
class User: # @staticmethod # def login(a, b): # print("登錄邏輯", a, b) # # 在函數的內部既不會用到self變量,也不會用到cls類 # 自己是一個普通的函數,被挪到類的內部執行,那麼直接給這個函數添加@staticmethod裝飾器就能夠了
調用方法:
# class User: # @staticmethod # def login(a, b): # print("登錄邏輯", a, b) # # 在函數的內部既不會用到self變量,也不會用到cls類 # # obj = User() # User.login(1, 2) # obj.login(3, 4)
class A: country = '中國' def func(self): print(self.__dict__) @classmethod def clas_func(cls): print(cls) @staticmethod def stat_func(): print("普通函數") @property def name(self): return 'wahah' # 能定義到類中的內容 # 靜態變量 是個全部的對象共享的變量 有對象\類調用 可是不能從新賦值 # 綁定方法 是個自帶self參數的函數 由對象調用 # 類方法 是個自帶cls參數的函數 由對象\類調用 # 靜態方法 是個啥都不帶的普通函數 由對象\類調用 # property屬性 是個假裝成屬性的方法 由對象調用 但不加括號
一些內置的魔術方法
___new___ class A: def __new__(cls, *args, **kwargs): o = object.__new__(cls) print("執行new", o) return o def __init__(self): print('執行initt', self) A() # 執行new <__main__.A object at 0x0000002F1E569048> # 執行initt <__main__.A object at 0x0000002F1E569048> # 實例化的時候, # 先建立一塊對象的空間,有個指針能指向類 --》 __new__ # 調用init--> __init__ # 設計模式 -- 單例模式 # 一個類,從頭至尾只會建立一次self的空間 class Baby: __instance = None def __new__(cls, *args, **kwargs): if cls.__instance is None: # cls.__instance = super().__new__(cls) cls.__instance = object.__new__(cls) return cls.__instance def __init__(self, cloth, pants): self.cloth = cloth self.pants = pants b1 = Baby('紅毛衣', '綠褲子') print(b1.cloth) b2 = Baby('白襯衫', '黑褲子') print(b1.cloth) print(b2.cloth) # 單例模式2.0: class Baby: def __init__(self, cloth, pants): self.cloth = cloth self.pants = pants b1 = Baby('紅上衣', '綠褲子') # 經過模塊引用的方式 # from 單例模式 import b1 # from 單例模式 import b1
__cal__ """ Call self as a function. """ # class A: # pass # # obj = A() # print(callable(obj)) # False class A: def __call__(self, *args, **kwargs): print('___', args) obj = A() print(callable(obj)) obj() # True # ___ ()
__len__ class Cls: def __init__(self, name): self.name = name self.student = [] def len(self): return len(self.student) def __len__(self): return len(self.student) py22 = Cls('py22') py22.student.append('abc') py22.student.append('123') py22.student.append('qaz') print(len(py22)) class Pow: def __init__(self, n): self.n = n def __pow2__(self): return self.n ** 2 def pow2(obj): return obj.__pow2__() obj = Pow(10) print(pow2(obj))
__str__ class Course: # def __init__(self, name, price, period): # self.name = name # self.price = price # self.period = period # # def __str__(self): # return self.name # # python = Course('python', 21800, '6 month') # linux = Course('linux', 19800, '5 month') # mysql = Course('mysql', 12800, '3 month') # go = Course('go', 15800, '4 month') # print(go) # # go class cls: def __init__(self): self.student = [] def append(self, name): self.student.append(name) def __str__(self): return str(self.student) py22= cls() py22.append('大壯') print("咱們py22班 %s" %py22) print(py22) py22.append('小狀') print(py22) # 在打印一個對象的時候 調用__str__方法 # 在%s拼接一個對象的時候 調用__str__方法 # 在str一個對象的時候 調用__str__方法
__repr__ py22 = clas() py22.append('大壯') print(py22) print(str(py22)) print('咱們py22班 %s'%py22) print('咱們py22班 %r'%py22) print(repr(py22)) # 當咱們打印一個對象 用%s進行字符串拼接 或者str(對象)老是調用這個對象的__str__方法 # 若是找不到__str__,就調用__repr__方法 # __repr__不只是__str__的替代品,還有本身的功能 # 用%r進行字符串拼接 或者用repr(對象)的時候老是調用這個對象的__repr__方法