-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescriptor.py
More file actions
37 lines (28 loc) · 822 Bytes
/
descriptor.py
File metadata and controls
37 lines (28 loc) · 822 Bytes
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
# In The Name Of God
# ========================================
# [] File Name : descriptor.py
#
# [] Creation Date : 19-10-2016
#
# [] Created By : Parham Alvani (parham.alvani@gmail.com)
# =======================================
class RevealAccess(object):
"""
A data descriptor that sets and returns values
normally and prints a message logging their access.
"""
def __init__(self, initval=None, name="var"):
self.val = initval
self.name = name
def __get__(self, obj, objtype):
print("Retrieving", self.name)
return self.val
def __set__(self, obj, val):
print("Updating", self.name)
self.val = val
class MyClass(object):
def __init__(self):
self.x = RevealAccess(10, 'var "x"')
self.y = 5
m = MyClass()
print(m.x)