class Basis:
def __init__(self, M, N, T, U, lat):
self.M = M
self.N = N
self.lat = lat
self.U = U
self.T = T
def __init__(self, M, N, T, U, lat=None):
self.M = M
self.N = N
self.lat = lat
self.U = U
self.T = T
Here we see two init functions. What are the differences? Is the first function redudant?
In fact, that’s an important point to note. Revise the first __init__ method as follows, and execute the script without even instantiating a Basis object:
def __init__(self, M, N, T, U, lat, V=print("Hi There!")):
self.M = M
self.N = N
self.T = T
self.U = U
self.lat = lat
self.V = V
Output:
Hi There!
While the Hi There! output is only a minor side effect, it does demonstrate that during its brief life, a mischievous method or function that immediately gets overwritten without ever having been called could have side effects. The above is, of course, not good coding style.
I would consider that an implementation detail of the current version of CPython, though. There isn’t necessarily a reason to store the old function (even though it still needs to be defined to preserve the semantics of the original), aside from the compilation cost of verifying it can be safely discarded.