OOP in three languages
In Python, we can create instance by defining custom factory function. If a class is defined, you can create an instance by
instance = Class() . Python interpreter will actually call
Class().method(instance) and execute method defined in that class. Don't forget to include
self in the definition of methods so Python knows which instance to pass for executing the method.
However such prototype assignment will result wrong
instanceof results. For example, in the above case the return values of
rob instanceof Superman and
rob instanceof Incredibles would be both true, because the constructor of the Incredibles points to Superman after the prototype assignment. Some people suggest to use
Incredibles.prototype.constructor = Incredibles after the prototype assignment to correct the wrong chaining.