@@ -9,14 +9,15 @@ def __init__(self, x, y): # like c++ constructor
99 self .x = x
1010 self .y = y
1111 self .health = 100
12-
12+ '''
1313 def __init__(self): # check about default constructor
1414 self.x =0
1515 self.y=0
16+ '''
1617 def move (self , dx , dy ): # self is the instance or similar to this pointer in c++
1718 self .x += dx
1819 self .y += dy
19-
20+
2021 def damage (self , pts ):
2122 self .health -= pts
2223
@@ -59,6 +60,25 @@ def damage(self, pts):
5960a = Stock ('goo' ,11 ,22.22 )
6061b = Stock ('uuu' ,66 ,77.23 )
6162
63+ #-------------------------------------------
64+ # importing Tlogs module and printing....
65+ import Work .meslog as meslog
66+ dataDir = os .getcwd ()
67+ dataDir += r'\work\Data\meslogs.txt'
68+ mylogs = meslog .ReadMesLogs (dataDir )
69+
70+
71+ for log in mylogs :
72+ #print(log.Id)
73+ #str(log)
74+ for sec in log .sections :
75+ #print(sec.z,sec.x,sec.y,sec.d)
76+ print (str (sec ))
77+ print ('---------------' )
78+
79+
80+ #----------------------------------
81+
6282stc = [a ,b ]
6383stc
6484for s in stc :
@@ -101,28 +121,94 @@ def damage(self, pts):
101121#Exercise 4.4: Using your class
102122# inside report.py
103123
104- ''' 4.2 inheritance
105- class Parent:
106- ...
107124
108- class Child(Parent):
109- >>> method overriding can be done
110- using "super" to call the base class method
111125
112- class Stock:
126+ '''
127+ ########### 4.2 inheritance
128+ class Parent:
129+
130+
131+ class Child(Parent):
132+ >>> with Inheritance
133+ Extending : With inheritance, you are taking an existing class and:
134+ Adding new methods
135+ Redefining some of the existing methods
136+ Adding new attributes to instances
137+
138+ >>> method overriding can be done
139+ using "super" to call the base class method
140+
141+ class Stock:
142+ ...
143+ def cost(self):
144+ return self.shares * self.price
145+ ...
146+ class MyStock(Stock):
147+ def cost(self):
148+ # Check the call to `super`
149+ actual_cost = super().cost() ## using super ...
150+ return 1.25 * actual_cost
151+
152+ >>> if __init__ is redifined in derived class, it is essential to initialize parent
153+ call the __init__() method on the super which is the way to call the base version as shown previously.
154+ >>> object as base class - if a class has no parent, object can be used as a base class. "object" is the parent of all objects in Python
155+ >>> Multiple inheritance
156+ class Mother:
113157 ...
114- def cost(self):
115- return self.shares * self.price
158+ class Father:
116159 ...
160+ class child (Mother, Father):
161+ '''
162+
163+ '''
164+ Exercise 4.5: An Extensibility Problem
165+ source file tableformat.py
166+ abstract class; only declaration for extensibility
167+ Exercise 4.7: Polymorphism in Action
168+
169+ '''
170+
171+ from stock import Stock ,MyStock
172+
173+ ms = MyStock (20 ,120 ,2 )
174+ print (ms .cost ())
175+
176+ s2 = ms .cost
177+ s2
178+ s2 ()
179+
180+ '''
181+ 4.3 Special Methods
182+ -------------------
183+ Classes may define special methods which are treated special way by the python interpreter.
184+ Special methods are always preceded by __; eg: __init(self):
185+ '''
186+ from datetime import date
187+ d = date (2024 ,11 ,12 )
188+ print (d )
189+
190+ str ("str" )
191+ str (d )
192+ repr (d ) # __repr__
117193
118- class MyStock(Stock):
119- def cost(self):
120- # Check the call to `super`
121- actual_cost = super().cost() ## using super ...
122- return 1.25 * actual_cost
194+ #todo:: try using special function str and __len__; __getitem__()
123195
124- >>> if __init__ is redifined in derived class, it is essential to initialize parent
125- call the __init__() method on the super which is the way to call the previous version as shown previously.
196+ # lookup and bound methods
197+ lg1 = meslog .TSec (2 ,3 ,4 ,5 )
198+ lg1
199+ lg1 ()
126200
201+ ## Attribute Access
127202'''
203+ property getter and setter
204+ '''
205+
206+ ###### 4.4 Defining Exceptions
207+
208+ class NetworkError (Exception ):
209+ pass
210+
211+ class AuthenticationError (NetworkError ):
212+ pass
213+
128214
0 commit comments