Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5daa72

Browse filesBrowse files
author
Saeid Darvish
committed
create l13
1 parent 63a426e commit f5daa72
Copy full SHA for f5daa72

File tree

Expand file treeCollapse file tree

2 files changed

+115
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+115
-0
lines changed
Open diff view settings
Collapse file

‎index.rst‎

Copy file name to clipboardExpand all lines: index.rst
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
lessons/l10
2828
lessons/l11
2929
lessons/l12
30+
lessons/l13
3031
log
3132
donate
3233

Collapse file

‎lessons/l13.rst‎

Copy file name to clipboard
+114Lines changed: 114 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
.. role:: emoji-size
2+
3+
.. meta::
4+
:description: کتاب آنلاین و آزاد آموزش زبان برنامه‌نویسی پایتون به فارسی - درس سیزدهم تابع
5+
:keywords: آموزش, آموزش پایتون, آموزش برنامه نویسی, پایتون, انواع شی, انواع داده, پایتون
6+
7+
8+
درس ۱۳: تابع - بخش دوم
9+
========================
10+
11+
12+
13+
14+
15+
16+
17+
18+
:emoji-size:`` سطح: متوسط
19+
20+
----
21+
22+
23+
.. contents:: سرفصل‌ها
24+
:depth: 2
25+
26+
----
27+
28+
29+
30+
31+
Decorator (تزئین‌گر)
32+
--------------------
33+
34+
35+
تزئین‌گرها یا همان **Decorator‌** ها توابعی هستند که به منظور پوشش (wrap) توابع یا کلاس‌های دیگر پیاده‌سازی می‌شوند. Decorator‌ها در پایتون ابزار بسیار کاربردی و مفیدی هستند که به برنامه‌نویس این امکان را می‌دهند تا بدون تغییر در بدنه توابع و کلاس‌های خود، رفتار و ویژگی‌های آن‌ها را گسترش دهد.
36+
37+
برای پوشش یک تابع توسط Decorator‌ از سینتکسی مشابه ``decorator‌_name@`` در بالای بخش سرآیند استفاده می‌شود:
38+
39+
::
40+
41+
@decorator_name
42+
def function_name():
43+
print("Somthing!")
44+
45+
46+
function_name()
47+
48+
مفهومی که این سینتکس (``decorator‌_name`` + ``@``) در بالای بخش سرآیند یک تابع برای مفسر پایتون ایجاد می‌کند کاملا مشابه با سینتکس پایین است::
49+
50+
wrapper = decorator_name(function_name)
51+
wrapper()
52+
53+
هر چیزی در پایتون یک شی است حتی مفاهیم پیچیده‌ای به مانند تابع؛ از درس پیش نیز به خاطر داریم که تابع در پایتون یک موجودیت **”first-class“** است که یعنی می‌توان تابع را مانند دیگر اشیا به صورت آرگومان به توابع دیگر ارسال نمود. نمونه کد بالا نیز نمایش ارسال یک تابع (``function_name``) به تابعی دیگر (``decorator‌_name``) است.
54+
55+
56+
به مثال پایین توجه نمایید:
57+
58+
::
59+
60+
>>> def decorator_name(func):
61+
... def wrapper():
62+
... print("Something is happening before the function is called.")
63+
... func()
64+
... print("Something is happening after the function is called.")
65+
... return wrapper
66+
...
67+
>>>
68+
>>> @decorator_name
69+
... def function_name():
70+
... print("Somthing!")
71+
...
72+
>>>
73+
>>> function_name()
74+
Something is happening before the function is called.
75+
Somthing!
76+
Something is happening after the function is called.
77+
>>>
78+
79+
نمونه کد بالا را می‌توان با ساختار ساده زیر نیز در نظر گرفت:
80+
81+
::
82+
83+
>>> def decorator_name(func):
84+
... def wrapper():
85+
... print("Something is happening before the function is called.")
86+
... func()
87+
... print("Something is happening after the function is called.")
88+
... return wrapper
89+
...
90+
>>>
91+
>>> def function_name():
92+
... print("Somthing!")
93+
...
94+
>>>
95+
>>> wrapper = decorator_name(function_name)
96+
>>> wrapper()
97+
Something is happening before the function is called.
98+
Somthing!
99+
Something is happening after the function is called.
100+
>>>
101+
102+
همانطور که با مقایسه دو نمونه کد بالا قابل مشاهده است، Decorator‌ها یک روپوش (wrapper) برای توابع و کلاس‌های ما بوجود می‌آورند. در هنگام فراخوانی تابع ``function_name`` مفسر پایتون متوجه decorator‌ آن شده است و به جای اجرا، یک نمونه شی از آن را به decorator‌ مشخص شده (``decorator‌_name``) ارسال می‌کند و یک شی جدید که در اینجا با عنوان ``wrapper`` مشخص شده است را دریافت و اجرا می‌کند.
103+
104+
105+
106+
107+
|
108+
109+
----
110+
111+
:emoji-size:`😊` امیدوارم مفید بوده باشه
112+
113+
114+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.