Skip to content

Commit d5c966c

Browse files
committed
Pthon Decorators
1 parent 0a808d9 commit d5c966c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Programs/P46_Decorators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Author: OMKAR PATHAK
22
# In this example program we will see how decorators work in Python
33

4+
# Decorators provide a simple syntax for calling higher-order functions. By definition,
5+
# a decorator is a function that takes another function and extends the behavior of the
6+
# latter function without explicitly modifying it. Sounds confusing – but it's really not,
7+
# especially after we go over a number of examples.
8+
49
def decorator(myFunc):
510
def insideDecorator(*args):
611
print('insideDecorator Function executed before {}'.format(myFunc.__name__))

Programs/P47_MoreOnDecorators.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Author: OMKAR PATHAK
2+
# In this example, we will be seeing some more concepts of decorators such as
3+
# property decorator, getters and setters methods.
4+
5+
class BankAccount(object):
6+
def __init__(self, firstName, lastName):
7+
self.firstName = firstName
8+
self.lastName = lastName
9+
10+
@property # property decorator
11+
def fullName(self):
12+
return self.firstName + ' ' + self.lastName
13+
14+
@fullName.setter
15+
def fullName(self, name):
16+
firstName, lastName = name.split(' ')
17+
self.firstName = firstName
18+
self.lastName = lastName
19+
20+
if __name__ == '__main__':
21+
acc = BankAccount('Omkar', 'Pathak')
22+
print(acc.fullName) # Notice that we can access the method for our class BankAccount without
23+
# parenthesis! This is beacuse of property decorator
24+
25+
# acc.fullName = 'Omkar Pathak' #This throws an error! Hence setter decorator should be used.
26+
acc.fullName = 'Jagdish Pathak'
27+
print(acc.fullName)

0 commit comments

Comments
 (0)