為什么在Python代碼中需要裝飾器

Python is praised for its clarity and syntactic sugariness. In this article, I will teach you to use decorators in Python to make your code readable and clean.

Python的清晰性和語法含糖度受到贊譽。 在本文中,我將教您在Python中使用裝飾器,以使您的代碼更具可讀性和簡潔性。

什么是裝飾器? (What Are Decorators?)

To understand what decorators are, you first need to be familiar with the way Python handles functions. From its point of view, functions are no different than regular objects. They have properties and can be reassigned:

要了解裝飾器是什么,首先需要熟悉Python處理函數的方式。 從它的角度來看,功能與常規對象沒有什么不同。 它們具有屬性,可以重新分配:

Moreover, you can pass them as arguments to other functions:

此外,您可以將它們作為參數傳遞給其他函數:

Now, to decorators. A decorator is used to modify the behaviour of a function or class. The way this is achieved is by defining a function (decorator) that returns another function. This sounds complicated, but you will understand everything with this example:

現在,到裝飾員。 裝飾器用于修改函數或類的行為。 實現此方法的方法是定義一個函數(裝飾器),該函數返回另一個函數。 這聽起來很復雜,但是您將通過此示例理解所有內容:

Let’s go step by step:

讓我們一步一步走:

  • Firstly, we define the logging_decorator function on line 1. It accepts a single argument, which is the function we are trying to decorate.

    首先,我們在第1行上定義logging_decorator函數。它接受一個參數,這是我們嘗試修飾的函數。

  • Inside, we define another function: the logging_wrapper. The logging_wrapper is then returned and used in place of the original decorated function.

    在內部,我們定義了另一個函數: logging_wrapper 。 然后返回logging_wrapper并代替原始的裝飾函數。

  • On line 7, you can see how the decorator is applied to the sum function.

    在第7行,您可以看到裝飾器如何應用于sum函數。

  • On line 11, when we call sum, it will not just call sum. It will call the logging_wrapper, which will log before and after calling the sum.

    在第11行,當我們調用sum ,它不僅會調用sum 。 它將調用logging_wrapper ,它將在調用sum之前和之后進行記錄。

為什么需要裝飾器? (Why Do You Need Decorators?)

It is simple: readability. Python is praised for its clear and concise syntax, and decorators are no exceptions. If there is any behaviour that is common to more than one function, you probably need to make a decorator. Here are some examples of when they might come in handy:

很簡單:可讀性。 Python因其簡潔明了的語法而廣受贊譽,裝飾器也不例外。 如果有多個功能共有的行為,則可能需要制作裝飾器。 以下是一些可能派上用場的示例:

  • Checking argument type at runtime

    在運行時檢查參數類型
  • Benchmark function calls

    基準函數調用
  • Cache function results

    緩存功能結果
  • Count function calls

    計算函數調用
  • Checking metadata (permissions, roles, etc.)

    檢查元數據(權限,角色等)
  • Metaprogramming

    元編程
  • And much more…

    以及更多…

Now I will list some code examples.

現在,我將列出一些代碼示例。

具有返回值的裝飾器 (Decorators With Return Values)

Suppose we want to know how long each function call takes. Also, functions return something most of the time, so the decorator must handle that as well:

假設我們想知道每個函數調用要花費多長時間。 而且,函數大多數時候會返回某些東西,因此裝飾器也必須處理該問題:

You can see we store the returned value in result on line 5. But before returning it, we have to finish timing the function. This is an example of behaviour that would not be possible without decorators.

您可以看到我們將返回的值存儲在第5行的result中。但是在返回它之前,我們必須完成對函數的計時。 這是沒有裝飾器就無法實現的行為示例。

帶參數的裝飾器 (Decorators With Arguments)

Sometimes, we want a decorator that accepts values (like @app.route('/login') in Flask):

有時,我們需要一個接受值的裝飾器(例如Flask中的@app.route('/login') ):

In order to achieve that, we defined an extra function that accepts an argument and returns a decorator.

為了實現這一點,我們定義了一個額外的函數,該函數接受一個參數并返回一個裝飾器。

用課堂裝飾 (Decorating With Classes)

It is possible to decorate using classes instead of functions. The only difference is the syntax, so do what you are more comfortable with. Here is the logging decorator rewritten using classes:

可以使用類而不是函數進行裝飾。 唯一的區別是語法,所以您更喜歡它。 這是使用類重寫的日志裝飾器:

The upside is that you do not have to deal with nested functions. All you need to do is define a class and override the __call__ method.

好處是您不必處理嵌套函數。 您需要做的就是定義一個類并覆蓋__call__方法。

裝飾類 (Decorating Classes)

There may be times when you want to decorate each and every method in a class. You could always write it like this:

有時您可能想裝飾一個類中的每個方法。 您總是可以這樣寫:

But if you have lots of methods, this can get out of hand. Thankfully, there is a way to decorate the whole class at once:

但是,如果您有很多方法,這可能會一發不可收拾。 值得慶幸的是,有一種方法可以一次裝飾整個類:

Now, do not panic. This looks complicated, but this is the same logic:

現在,不要驚慌。 這看起來很復雜,但這是相同的邏輯:

  • Firstly, we leave the logging_decorator as is. It will be applied to all methods of a class.

    首先,我們保持logging_decorator 。 它將應用于類的所有方法。

  • Then we define a new decorator: log_all_class_methods. It is like a regular decorator but returns a class instead.

    然后,我們定義一個新的裝飾器: log_all_class_methods 。 它就像一個普通的裝飾器,但是返回一個類。

  • The NewCls has a custom __getattribute__. For all calls to the original class, it will decorate the functions with the logging_decorator.

    NewCls有一個自定義__getattribute__ 。 對于所有對原始類的調用,它將使用logging_decorator裝飾函數。

內置裝飾器 (Built-In Decorators)

Not only can you define your own decorators, but there are some shipped in the standard library as well. I will list the three that I have worked with the most:

您不僅可以定義自己的裝飾器,而且標準庫中也有一些裝飾器。 我將列出我工作最多的三個人:

  • @property — A decorator from built-ins that lets you define getters and setters for class properties.

    @property property-內置的裝飾器,可讓您定義類屬性的getter和setter。

  • @lru_cache — A decorator from the functools module. It memorizes function arguments and return values, which is handy for pure functions (like the factorial).

    @lru_cachefunctools模塊中的裝飾器。 它存儲函數參數和返回值,這對于純函數(例如factorial )非常方便。

  • @abstractmethod — A decorator from the abc module. Indicates that the method is abstract and implementation details are missing.

    @abstractmethod —來自abc模塊的裝飾器。 表示該方法是抽象的,并且缺少實現細節。

結束語 (Closing Notes)

Thank you for reading, I hope you liked my article. Stay subscribed for more Python content!

感謝您的閱讀,希望您喜歡我的文章。 請繼續訂閱更多Python內容!

資源資源 (Resources)

  • PEP 318 — Decorators for Functions and Methods

    PEP 318 —功能和方法的裝飾器

  • Higher-order functions and operations on callable objects

    可調用對象的高階函數和操作

翻譯自: https://medium.com/better-programming/why-you-need-decorators-in-your-python-code-df12d43eac9c

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/387998.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387998.shtml
英文地址,請注明出處:http://en.pswp.cn/news/387998.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Elasticsearch Reference [6.7] ? Modules ? Network Settings

2019獨角獸企業重金招聘Python工程師標準>>> Search Settings Node Network Settingsedit Elasticsearch binds to localhost only by default. This is sufficient for you to run a local development server (or even a development cluster, if you star…

【百度】大型網站的HTTPS實踐(一)——HTTPS協議和原理

大型網站的HTTPS實踐(一)——HTTPS協議和原理 原創 網絡通信/物聯網 作者:AIOps智能運維 時間:2018-11-09 15:07:39 349 0前言 百度于2015年上線了全站HTTPS的安全搜索,默認會將HTTP請求跳轉成HTTPS。從今天開始&…

數據清理最終實現了自動化

蘋果 | GOOGLE | 現貨 | 其他 (APPLE | GOOGLE | SPOTIFY | OTHERS) Editor’s note: The Towards Data Science podcast’s “Climbing the Data Science Ladder” series is hosted by Jeremie Harris. Jeremie helps run a data science mentorship startup called Sharpest…

mui 與jquery 同時使用,$沖突解決辦法。

(function($,doc,$$) { 。。。。。 }(mui, document, jQuery)); 使用$$代替jQuery。 var $$jQuery.noConflict();此方法也可以 轉載于:https://www.cnblogs.com/mustanglqt/p/10608499.html

LVS原理介紹及安裝過程

一、ARP技術概念介紹 為什么講ARP技術,因為平常工作中有接觸。還有就是LVS的dr模式是用到arp的技術和數據。 1、什么是ARP協議 ARP協議全程地址解析協議(AddressResolution Protocol,ARP)是在僅知道主機的IP地址時確定其物理地…

Python氣流介紹

This is a memo to share what I have learnt in Apache Airflow, capturing the learning objectives as well as my personal notes. The course is taught by Mike Metzger from DataCamp.這是一份備忘錄,旨在分享我在Apache Airflow中學到的知識,記錄…

java~springcloud微服務目錄索引

回到占占推薦博客索引 最近寫了不過關于java,spring,微服務的相關文章,今天把它整理一下,方便大家學習與參考。 java~springcloud微服務~目錄索引 springcloud~服務注冊與發現Eureka的使用 springcloud~配置中心的使用 springclou…

DNS Bind9在windows7下

有些公司技術力量薄弱一些,一直在用windows系統,所以本文從windows出發,安裝bind,利用它的view功能,做智能DNS,解決雙線機房南北電信聯通訪問問題前言: 搞LINUX的朋友都知道,bind是l…

正確的詞典訪問方式

unity3d 詞典訪問Python字典指南 (Python Dictionary Guide) The dictionary is one of the data structures that are ready to use when programming in Python.字典是使用Python進行編程時可以使用的數據結構之一。 在我們開始之前,什么是字典? (Bef…

Vue.js(5)- 全局組件

全局組件 定義組件的語法 Vue.component(組件的名稱, { 組件的配置對象 }) 在組件的配置對象中:可以使用 template 屬性指定當前組件要渲染的模板結構; 使用組件的語法 把 組件的名稱, 以標簽的形式,引入到頁面上就行; // 導入v…

DNS的幾個基本概念:

一. 根域 就是所謂的“.”,其實我們的網址www.baidu.com在配置當中應該是www.baidu.com.(最后有一點),一般我們在瀏覽器里輸入時會省略后面的點,而這也已經成為了習慣。 根域服務器我們知道有13臺&#xff…

廢水處理計算書 excel_廢水監測數據是匿名的嗎?

廢水處理計算書 excelOur collective flushes help track and respond to Covid-19 and so much more. Your body waste contains harvestable compounds that can reveal your illnesses and diseases, consumption habits, and cosmetic use. Researchers gain insights from…

文件在線預覽 圖片 PDF Excel Word

1、前端實現pdf文件在線預覽功能 方式一、pdf文件理論上可以在瀏覽器直接打開預覽但是需要打開新頁面。在僅僅是預覽pdf文件且UI要求不高的情況下可以直接通過a標簽href屬性實現預覽 <a href"文檔地址"></a> 2、word、xls、ppt文件在線預覽功能 word、pp…

數據科學還是計算機科學_您應該擁有數據科學博客的3個原因

數據科學還是計算機科學“Start a Blog to cement the things you learn. When you teach what you’ve learned in the form of a blog you can see the gaps in your knowledge and fill them in” — My Manager (2019)“創建一個博客以鞏固您所學到的東西。 當您以博客的形…

D3.js 加標簽

條形圖還可以配上實際的數值,我們通過文本元素添加數據值。 svg.selectAll("text").data(dataset).enter().append("text").text(function(d){return d;}) 通過 x 和 y 值來定位文本元素。 .attr("text-anchor", "middle").attr("…

oppo5.0以上機器(親測有效)激活Xposed框架的教程

對于喜歡玩手機的朋友而言&#xff0c;常常會用到xposed框架以及種類繁多功能強大的模塊&#xff0c;對于5.0以下的系統版本&#xff0c;只要手機能獲得ROOT權限&#xff0c;安裝和激活xposed框架是異常簡便的&#xff0c;但隨著系統版本的迭代&#xff0c;5.0以后的系統&#…

和matlab一樣的輕量級

Python&#xff08;英國發音&#xff1a;/?pa?θ?n/ 美國發音&#xff1a;/?pa?θɑ?n/&#xff09;, 是一種面向對象、解釋型計算機程序設計語言&#xff0c;由Guido van Rossum于1989年發明&#xff0c;第一個公開發行版發行于1991年。Python是純粹的自由軟件&#xff…

熊貓分發_流利的熊貓

熊貓分發Let’s uncover the practical details of Pandas’ Series, DataFrame, and Panel讓我們揭露Pandas系列&#xff0c;DataFrame和Panel的實用細節 Note to the Readers: Paying attention to comments in examples would be more helpful than going through the theo…

redis tomcat session

本機ip為192.168.1.101 1、準備測試環境 兩個Tomcat 在Eclipse中新建2個Servers&#xff0c;指定對應的Tomcat&#xff0c;端口號錯開。 Tomcat1&#xff08;18005、18080、18009&#xff09; Tomcat2&#xff08;28005、28080、28009&#xff09; 一個Redis Redis下載官網&…

Fiddler抓包-只抓APP的請求

from:https://www.cnblogs.com/yoyoketang/p/6582437.html fiddler抓手機app的請求&#xff0c;估計大部分都會&#xff0c;但是如何只抓來自app的請求呢&#xff1f; 把來自pc的請求過濾掉&#xff0c;因為請求太多&#xff0c;這樣會找不到重要的信息了。 環境準備&#xff1…