File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 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+
49def decorator (myFunc ):
510 def insideDecorator (* args ):
611 print ('insideDecorator Function executed before {}' .format (myFunc .__name__ ))
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments