-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoratorT.py
More file actions
180 lines (119 loc) · 3.47 KB
/
Copy pathdecoratorT.py
File metadata and controls
180 lines (119 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# ==========================================================
# FUNCTION DECORATORS IN PYTHON
# ==========================================================
# A decorator is a function that modifies another function
# without changing its actual code
# ==========================================================
# BASIC DECORATOR SYNTAX
# ==========================================================
def decorator(func): # function to be decorated
def wrapper():
print("Before function call")
func() # original function
print("After function call")
return wrapper
# applying decorator manually
def greet():
print("Hello")
greet = decorator(greet) # function = decorator(function)
greet()
# ==========================================================
# USING @ SYNTAX (SUGAR)
# ==========================================================
@decorator
def say_hi():
print("Hi")
say_hi()
# ==========================================================
# DECORATOR WITH ARGUMENTS
# ==========================================================
def decorator(func):
def wrapper(*args, **kwargs):
print("Before execution")
result = func(*args, **kwargs)
print("After execution")
return result
return wrapper
@decorator
def add(a, b):
return a + b
print(add(2, 3))
# ==========================================================
# CHAINING MULTIPLE DECORATORS
# ==========================================================
def deco1(func):
def wrapper():
print("Deco1")
func()
return wrapper
def deco2(func):
def wrapper():
print("Deco2")
func()
return wrapper
@deco1
@deco2
def test():
print("Function")
# Equivalent to:
# test = deco1(deco2(test))
test()
# ==========================================================
# BUILT-IN DECORATORS
# ==========================================================
# ----------------------------------------------------------
# 1. @classmethod
# ----------------------------------------------------------
class MyClass:
count = 0
def __init__(self):
MyClass.count += 1
@classmethod
def show_count(cls): # cls instead of self
print("Count:", cls.count)
MyClass()
MyClass()
MyClass.show_count()
# ----------------------------------------------------------
# 2. @staticmethod
# ----------------------------------------------------------
class Math:
@staticmethod
def add(a, b):
return a + b
print(Math.add(5, 3))
# ----------------------------------------------------------
# 3. @property
# ----------------------------------------------------------
class Person:
def __init__(self, name):
self._name = name
@property
def name(self): # getter
return self._name
@name.setter
def name(self, value): # setter
self._name = value
@name.deleter
def name(self): # deleter
del self._name
p = Person("Alice")
print(p.name) # getter
p.name = "Bob" # setter
print(p.name)
# ==========================================================
# SUMMARY (IN COMMENTS)
# ==========================================================
# decorator:
# function that wraps another function
# syntax:
# function = decorator(function)
# OR use @decorator
# wrapper function:
# adds extra behavior
# @classmethod:
# works with class (cls)
# @staticmethod:
# independent method (no self/cls)
# @property:
# used for getter/setter (encapsulation)