-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_mangling.py
52 lines (43 loc) · 1.05 KB
/
name_mangling.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
############## class attributes access control #############
''' single underscore: private variable but no interpreter block '''
class P:
def __init__(self):
self._x = 100
self.y = 200
def print(self):
print(self._x, self.y)
class C(P):
def __init__(self):
super().__init__()
self._x = 300
self.y = 400
d = C()
d.print() # 300 400
''' double underscore: private variable and interpreter (fake) block '''
class P:
def __init__(self):
self.__x = 100
self.y = 200
def print(self):
print(self.__x, self.y)
class C(P):
def __init__(self):
super().__init__()
self.__x = 300
self.y = 400
d = C()
d.print() # 100 400
''' double underscore workaround: name mangling '''
class P:
def __init__(self):
self.__x = 100
self.y = 200
def print(self):
print(self.__x, self.y)
class C(P):
def __init__(self):
super().__init__()
self._P__x = 300 # hacking access with name mangling: self._<classname>__attribute
self.y = 400
d = C()
d.print() # 300 400