forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsageProperty.py
53 lines (40 loc) · 1008 Bytes
/
UsageProperty.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
What @property decorator really is and how it works
"""
class UsagePropertyDeco(object):
def __init__(self, name_is=None):
print "Inside __init__"
self.name = name_is
def a(self):
print "inside A: 1"
def b(msg):
print "Inside B: 1"
return msg
print "Inside A: 2"
return b
@property
def c(self):
print "Inside C: 1"
return self.a()
class AlternateToPropertyDeco(object):
def __init__(self, name_is):
print "Inside __init__"
self.name = name_is
@property
def a(self):
print "Getter invoked"
return self.name
@a.setter
def a(self, val):
print "Setter invoked"
self.name = val
if __name__ == "__main__":
name = "Mayank"
obj = UsagePropertyDeco(name)
msg = "My name is mayank kumar"
print obj.c(msg)
print "*"*80
obj = AlternateToPropertyDeco(name)
print obj.a
obj.a = "maya"
print obj.a