【python】裝飾器!property和setter用法

      網友投稿 623 2025-04-03

      文章目錄


      1.引子:函數也是對象

      2.函數內的函數

      3.裝飾器小栗子

      5.property和setter用法

      reference

      1.引子:函數也是對象

      木有括號的函數那就不是在調用。

      def hi(name="yasoob"): return "hi " + name print(hi()) # output: 'hi yasoob' # 我們甚至可以將一個函數賦值給一個變量,比如 greet = hi # 我們這里沒有在使用小括號,因為我們并不是在調用hi函數 # 而是在將它放在greet變量里頭。我們嘗試運行下這個 print(greet()) # output: 'hi yasoob' # 如果我們刪掉舊的hi函數,看看會發生什么! del hi print(hi()) #outputs: NameError print(greet()) #outputs: 'hi yasoob'

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      2.函數內的函數

      (1)在python中,一個函數內能嵌套定義另一個函數,并且可以在該大函數內調用該小函數。

      def hi(name="yasoob"): print("now you are inside the hi() function") def greet(): return "now you are in the greet() function" def welcome(): return "now you are in the welcome() function" print(greet()) print(welcome()) print("now you are back in the hi() function") hi() #output:now you are inside the hi() function # now you are in the greet() function # now you are in the welcome() function # now you are back in the hi() function # 上面展示了無論何時你調用hi(), greet()和welcome()將會同時被調用。 # 然后greet()和welcome()函數在hi()函數之外是不能訪問的,比如: greet() #outputs: NameError: name 'greet' is not defined

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      (2)開始神奇的是,大函數的返回值可以是一個函數:

      def hi(name="yasoob"): def greet(): return "now you are in the greet() function" def welcome(): return "now you are in the welcome() function" if name == "yasoob": return greet #這里!! else: return welcome a = hi() print(a) #outputs: #上面清晰地展示了`a`現在指向到hi()函數中的greet()函數 #現在試試這個 print(a()) #outputs: now you are in the greet() function

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      在 if/else 語句中我們返回 greet 和 welcome,而不是 greet() 和 welcome()。

      為什么那樣?這是因為

      當你把一對小括號放在后面,這個函數就會執行;然而如果你不放括號在它后面,那它可以被到處傳遞,并且可以賦值給別的變量而不去執行它

      當我們寫下 a = hi(),hi() 會被執行,而由于 name 參數默認是 yasoob,所以函數 greet 被返回了。

      PS:如果我們打印出 hi()(),這會輸出 now you are in the greet() function。

      (3)最后要說的是函數作為參數傳入一個函數:

      def hi(): return "hi yasoob!" def doSomethingBeforeHi(func): print("I am doing some boring work before executing hi()") print(func()) doSomethingBeforeHi(hi) #outputs:I am doing some boring work before executing hi() # hi yasoob!

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      3.裝飾器小栗子

      終于來到了帶@的裝飾器,其實就是

      帶了@帽子的函數作為參數,傳入@后面的函數中

      def a_new_decorator(a_func): def wrapTheFunction(): print("I am doing some boring work before executing a_func()") a_func() print("I am doing some boring work after executing a_func()") return wrapTheFunction @a_new_decorator def a_function_requiring_decoration(): """Hey you! Decorate me!""" print("I am the function which needs some decoration to " "remove my foul smell") a_function_requiring_decoration() #outputs: I am doing some boring work before executing a_func() # I am the function which needs some decoration to remove my foul smell # I am doing some boring work after executing a_func() #the @a_new_decorator is just a short way of saying: a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      上面的代碼等價于我們熟悉的:

      def a_new_decorator(a_func): def wrapTheFunction(): print("I am doing some boring work before executing a_func()") a_func() print("I am doing some boring work after executing a_func()") return wrapTheFunction def a_function_requiring_decoration(): print("I am the function which needs some decoration to remove my foul smell") a_function_requiring_decoration() #outputs: "I am the function which needs some decoration to remove my foul smell" a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration) #now a_function_requiring_decoration is wrapped by wrapTheFunction() a_function_requiring_decoration() #outputs:I am doing some boring work before executing a_func() # I am the function which needs some decoration to remove my foul smell # I am doing some boring work after executing a_func()

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      不過一開始上面被裝飾過的函數名字已經悄悄發生“改變”,如果print下可以看出(如下代碼)。

      解決方案

      @wraps接受一個函數來進行裝飾,并加入了復制函數名稱、注釋文檔、參數列表等等的功能。這可以讓我們在裝飾器里面訪問在裝飾之前的函數的屬性。

      print(a_function_requiring_decoration.__name__) # Output: wrapTheFunction

      1

      2

      最終加上@wraps的代碼如下:

      from functools import wraps def a_new_decorator(a_func): @wraps(a_func) def wrapTheFunction(): print("I am doing some boring work before executing a_func()") a_func() print("I am doing some boring work after executing a_func()") return wrapTheFunction @a_new_decorator def a_function_requiring_decoration(): """Hey yo! Decorate me!""" print("I am the function which needs some decoration to " "remove my foul smell") print(a_function_requiring_decoration.__name__) # Output: a_function_requiring_decoration

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      5.property和setter用法

      class Timer: def __init__(self, value = 0.0): self._time = value self._unit = 's' # 使用裝飾器的時候,需要注意: # 1. 裝飾器名,函數名需要一直 # 2. property需要先聲明,再寫setter,順序不能倒過來 @property def time(self): return str(self._time) + ' ' + self._unit @time.setter def time(self, value): if(value < 0): raise ValueError('Time cannot be negetive.') self._time = value t = Timer() t.time = 1.0 print(t.time)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      【python】裝飾器!property和setter用法

      20

      21

      22

      reference

      https://www.runoob.com/w3cnote/python-func-decorators.html

      Python

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:陰影在哪找啊(陰影怎么去)
      下一篇:excel批注里怎么上傳圖片
      相關文章
      亚洲色偷偷综合亚洲av78| 亚洲一区二区影院| 亚洲女人初试黑人巨高清| 亚洲AV无码一区二区乱孑伦AS| 亚洲AV永久无码精品一区二区国产| 亚洲成AV人片高潮喷水| 亚洲日韩中文字幕无码一区| ASS亚洲熟妇毛茸茸PICS| 亚洲精品美女久久久久9999| 在线观看亚洲人成网站| 亚洲最大福利视频网站| 久久精品国产亚洲AV无码娇色| 久久久久亚洲av无码专区蜜芽| 亚洲精品无码99在线观看| 亚洲人成电影在线观看网| 亚洲理论片在线中文字幕| 亚洲精品欧洲精品| 亚洲第一香蕉视频| 亚洲人色大成年网站在线观看| 亚洲剧场午夜在线观看| 久久久久精品国产亚洲AV无码| 亚洲一区二区三区免费视频| 亚洲人成77777在线观看网| 亚洲永久网址在线观看| 亚洲国产无线乱码在线观看| 风间由美在线亚洲一区| 亚洲国产精品成人一区| 久久久久久A亚洲欧洲AV冫| 亚洲人成无码网站| 亚洲AV永久无码区成人网站| 91亚洲精品视频| 亚洲熟妇色自偷自拍另类| 亚洲一级在线观看| 亚洲乱亚洲乱妇无码| 在线观看亚洲免费| 亚洲中文字幕在线观看| 亚洲Aⅴ无码专区在线观看q| 亚洲第一页在线播放| 亚洲欧美国产国产综合一区| 亚洲AV中文无码乱人伦在线视色| 亚洲午夜福利精品无码|