-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced_features.py
215 lines (147 loc) · 4.43 KB
/
advanced_features.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""Implementing core syntax(operators,looping,etc.)
in our class design
Inheriting from build-ints(like lists, dictionaries,etc.)
@property: control attributes access
Attribute / variable naming conventions
With context. New-Style classes overview."""
"""lesson 1: core syntax"""
# var = 1
# var2 = 2
# print(var.__add__(var2)) # magic attribute
class SumList(object):
def __init__(self, this_list):
self.my_list = this_list
def __add__(self, other):
new_list = [x + y for x, y in zip(self.my_list, other.my_list)]
# following 4 lines same as line above
# new_list = []
# zipped_list = zip(self.mylist,other.mylist)
# for tup in zipped_list:
# new_list.append(tup[0] + tup[1])
return SumList(new_list)
def __repr__(self):
return str(self.my_list)
cc = SumList([1, 2, 3, 4, 5])
dd = SumList([100, 200, 300, 400, 500])
ee = cc + dd # cc.__add__(dd)
print ee # [101, 202, 303, 404, 505]
"""lesson 2: subclassing Built ins"""
class MyDict(dict):
def __setitem__(self, key, value):
print "setting a key and value!"
dict.__setitem__(self, key, value)
dd = MyDict()
dd['a'] = 5
dd['b'] = 6
for key in dd.keys():
print('{0}={1}'.format(key, dd[key]))
# MyList inherits from 'list' object but indexes from 1 instead of 0!
class MyList(list): # inherit from list
def __getitem__(self, index):
if index == 0: raise IndexError
if index > 0: index -= 1
return list.__getitem__(self, index) # this method is called when we access
# a value with subscript (x[1],etc.)
def __setitem__(self, key, value):
if index == 0: raise IndexError
if index > 0: index -= 1
list.__setitem__(self, index, value)
x = MyList(['a', 'b', 'c']) # __init__() inherited from builtin list
print x # __repr__() inherited from builtin list
x.append('spam') # append() inherited '' ... ''
print x[1] # 'a' (MyList.__get__item__
# customizes list superclass method)
print x[4] # index is 1, buf reflects 0!
# 'spam' (index is 4 bu reflects 3!)
"""lesson 3: Attribute Encapsulation"""
class GetSet(object):
def __init__(self, value):
self.attrval = value
@property
def var(self):
print "getting the 'var' attribute"
return self.attrval
@var.setter
def var(self, value):
print "setting the 'var' attribute"
self.attrval = value
@var.deleter
def var(self):
print "deleting the 'var' attribute"
self.attrval = None
me = GetSet(5)
me.var = 1000
print me.var
del me.var
print me.var
"""lesson 4: private variables"""
class GetSet(object):
instance_count = 0
__mangled_name = 'no privacy!'
def __init__(self, value):
self._attrval = value
GetSet.instance_count +=1
@property
def var(self):
print "getting the 'var' attribute"
return self._attrval
@var.setter
def var(self, value):
print "setting the 'var' attribute"
self._attrval = value
@var.deleter
def var(self):
print "deleting the 'var' attribute"
self._attrval = None
cc = GetSet(5)
cc.var = 10
print cc._attrval
print cc._GetSet__mangled_name
"""lesson 5: __with__ context """
# with open('filename.txt') as fh:
# for line in fh:
# line = line.rstrip()
# print(line)
#
# print 'done!'
#
# fh = open('filename.txt')
# for line in fh:
# print(line)
# fh.close()
class MyClass(object):
def __enter__(self):
print('we have entered "with"')
return self
def __exit__(self, type, value, traceback):
print('we a leaving "with"')
def say_hi(self):
print('hi, instance %s' % (id(self)))
with MyClass() as cc:
cc.say_hi()
print('after the "with" block')
class MyClass2(object):
def __enter__(self):
print('we have entered "with"')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('error type: {0}'.format(exc_type))
print('error type: {0}'.format(exc_val))
print('error type: {0}'.format(exc_tb))
def say_hi(self):
print('hi, instance %s' % (id(self)))
with MyClass2() as cc:
cc.say_hi()
print('after the "with" block')
"""lesson 6: New Style Classes"""
# old style "classic" class
class OldClass:
pass
# new style class
class NewClass(object):
pass
oc = OldClass()
nc = NewClass()
print(type(oc))
print(type(nc))
print oc.__class__