-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdecorators-class.py
36 lines (24 loc) · 934 Bytes
/
decorators-class.py
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
#class-decorators.py
# Till the previous examples, we saw function decorators.
# But decorators can be applied to Classes as well.
# This example deals with class decorators.
# NOTE: If you are creating a decorator for a class, you'll it
# to return a Class.
# NOTE: Similarly, if you are creating a decorator for a function,
# you'll need it to return a function.
def honirific(cls):
class HonirificCls(cls):
def full_name(self):
return "Ms." + super(HonirificCls, self).full_name()
return HonirificCls
@honirific
class Name(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def full_name(self):
return " ".join([self.first_name, self.last_name])
result = Name("Shikha", "Pandey").full_name()
print("Full name: {0}".format(result))
#Full name: Ms.Shikha Pandey
# This needs further check. Erroring out.