Subclassing -- replacing functions in a class¶
One of the main interests of using classes it the possibility of rewriting parts of an object. Here we take the Simul class and replace the __init__ and the md_step functions. We still have access to the previous definitions with the use of the super() call.
In [1]:
from simul import Simul
class ModifiedSimul(Simul):
"""
Subclassing documentation
"""
def __init__(self, sample_time, sigma, L):
super().__init__(sample_time, sigma, L)
print("ModifiedSimul init")
def md_step(self): # m number of iterations
print("sub md_step")
super().md_step() # we now call the original md_step code
simul = ModifiedSimul(L=4,sample_time=0.001, sigma=0.15)
simul.md_step()
print(simul.__doc__) # we have replaced the __doc__ string
print(simul)
ModifiedSimul init
sub md_step
Simul::md_step
Subclassing documentation
pos= [[1.00222495 1.0001973 ]
[4.00264478 1.00227013]
[0.99883407 3.99593497]
[3.99921956 3.99857134]]
vel= [[ 2.22494501 0.19729537]
[ 2.64477696 2.27012548]
[-1.16592854 -4.06503291]
[-0.78043509 -1.42866347]]
In [ ]: